2

我正在尝试在用户行走时从 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);
    } }

谁能帮我在用户步行时画一条线。上面只是从当前位置画一条线到某个地方

4

1 回答 1

2

我试图创建一个方法:

- 获得一个新的地理点

- 将其添加到道路上

- 删除之前的道路并显示新的道路,然后每 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);停止

希望有帮助

于 2015-04-29T09:25:41.497 回答