我正在尝试在Android地图中制作折线而用户走路下面是我的代码。但我只是从当前位置到其他地方直线。
public class Map extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;
GeoPoint geoPoint = null;
GeoPoint despoint = null;
int latitude;
int longitude;
GeoPoint srcGeoPoint;
public HelloItemizedOverlay routeOverlay;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main2); // bind the layout to the activity
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,
2, new GeoUpdateHandler());
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
latitude = (int) (location.getLatitude() * 1E6);
longitude = (int) (location.getLongitude() * 1E6);
}
srcGeoPoint = new GeoPoint(latitude, latitude);
geoPoint = srcGeoPoint;
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mapView.getController().setZoom(15);
//mapController.setCenter(geoPoint);
//mapView.getOverlays().add(new HelloItemizedOverlay(srcGeoPoint, srcGeoPoint));
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint changepoint = new GeoPoint(lat, lng);
mapView.getOverlays().add(new HelloItemizedOverlay(srcGeoPoint,changepoint));
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
super.onResume();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}}
重叠课程
public class HelloItemizedOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
public HelloItemizedOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(Color.BLUE);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(2);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
(float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
} }
任何人都可以帮助我在用户散步上划一条线。上面只是从当前位置画一条线到某些地方
答案 0 :(得分:2)
我试图创建一个方法:
-get a new GeoPoint
- 将它添加到路上
- 删除前一条路并显示新路 然后每10秒调用一次 这就是我编码的方式:
public void scheduleReceiveLocation(final GeoPoint newPosition) {
final int TEN_SECONDS = 10000;
handler.postDelayed(new Runnable() {
public void run () {
//remove previous point
waypoints.remove(1);
//delete the previous road
map.getOverlays().remove(0);
//add the new GeoPoint
waypoints.add(1,newPosition);
// draw the new road with the new position
new UpdateRoadTask().execute(waypoints);
// this method will contain your almost-finished HTTP calls
handler.postDelayed(this, TEN_SECONDS);
}
}, TEN_SECONDS);
}
然后只需致电handler.removeCallbacksAndMessages(null);
停止
希望有所帮助