1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| package com.anis.draw;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class DrawActivity extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
private List<Overlay> mapOverlays;
private Projection projection;
MapView mapView = null;
private LocationManager lm = null;;
private double lat = 0;
private double lng = 0;
private MapController mc = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) this.findViewById(R.id.mapView);
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this);
mc = mapView.getController();
mc.setZoom(17);
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_S)
{
mapView.setSatellite(!mapView.isSatellite());
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(getBaseContext(),
"Place change à : Latitude = " + lat + " Longitude = " + lng,
Toast.LENGTH_SHORT).show();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setCenter(p);
MonOverlay object = new MonOverlay(getResources().getDrawable(R.drawable.map)) ;
object.addpoint(p);
mapView.getOverlays().add(object);
mapView.setTraffic(true);
mapView.setSatellite(true);
}
public class MonOverlay extends ItemizedOverlay<OverlayItem>{
List<GeoPoint> points = new ArrayList<GeoPoint>();
public MonOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
GeoPoint point = points.get(i);
return new OverlayItem(point,"Titre","description");
}
@Override
public int size() {
return points.size();
}
public void addpoint(GeoPoint point){
this.points.add(point);
populate();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
protected boolean isRouteDisplayed() {
return false;
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
int x = (int) (lat * 1E6);
int y = (int) (lng * 1E6);
GeoPoint gp0 = new GeoPoint(x, y);
GeoPoint gP1 = new GeoPoint(10543211,331234567);
// GeoPoint gP2 = new GeoPoint(37423157, -122085008);
//GeoPoint gP3 = new GeoPoint(37423157, -121085008);
// GeoPoint gP4 = new GeoPoint(19423157, -121085008);
Point p0 = new Point();
Point p1 = new Point();
// Point p2 = new Point();
// Point p3 = new Point();
// Point p4 = new Point();
Path path = new Path();
projection.toPixels(gp0, p0);
projection.toPixels(gP1, p1);
// projection.toPixels(gP2, p2);
// projection.toPixels(gP3, p3);
// projection.toPixels(gP4, p4);
path.moveTo(p0.x, p0.y);
path.lineTo(p1.x,p1.y);
// path.moveTo(p3.x, p3.y);
//path.lineTo(p4.x,p1.y);
canvas.drawPath(path, mPaint);
}
}
} |
Partager