地图叠加不更新

时间:2013-02-27 09:11:36

标签: android google-maps gps overlay android-maps

我正试图绘制路径,因为农民在他的农场周围移动,下面是我的课程,我的问题是当我开始追踪线路时,即使用户没有移动而路径没有随着他的移动而更新

public class RouteOverlay extends Overlay {

private GeoPoint gp1;
private GeoPoint gp2;
private int mode = 1;

public RouteOverlay(GeoPoint paramGeoPoint1, GeoPoint paramGeoPoint2,int paramInt) 
{
    this.gp1 = paramGeoPoint1;
    this.gp2 = paramGeoPoint2;
    this.mode = paramInt;
}

public void draw(Canvas paramCanvas, MapView paramMapView,
        boolean paramShadow) 
{

    super.draw(paramCanvas, paramMapView, paramShadow);

    Projection projection = paramMapView.getProjection();
    Paint   mPaint = new Paint();
    mPaint.setDither(true);
   mPaint.setAntiAlias(true);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);
    mPaint.setAlpha(120);

    Point p1 = new Point();
    Point p2 = new Point();
    Path path = new Path();

    projection.toPixels(gp1, p1);
    projection.toPixels(gp2, p2);

    path.moveTo(p2.x,p2.y);
    path.lineTo(p1.x,p1.y);

    paramCanvas.drawPath(path, mPaint);
}

mainactivity:

public class MainActivity extends MapActivity {



public final LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        coordinates.add(location);
        mapView.getController().animateTo(getGeoByLocation(location));
        drawRoute(coordinates, mapView);

    }


};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_area_measurement);
    this.mapView = ((MapView) findViewById(R.id.mapview));
    this.mapView.setBuiltInZoomControls(false);
    this.mapView.getController().setZoom(17);
    this.coordinates = new ArrayList<Location>();

}

    protected boolean isRouteDisplayed() {
    return false;
}

private GeoPoint getGeoByLocation(Location location) {
    GeoPoint gp = null;

    try {
        if (location != null) {
            double geoLatitude = location.getLatitude() * 1E6;
            double geoLongitude = location.getLongitude() * 1E6;
            gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
        }

    } catch (Exception e) {
        e.printStackTrace(); 
    }
    return gp;
}

public String getLocationProvider(LocationManager paramLocationManager) {
    try {
        Criteria localCriteria = new Criteria();
        localCriteria.setAccuracy(1);
        localCriteria.setAltitudeRequired(false);
        localCriteria.setBearingRequired(false);
        localCriteria.setCostAllowed(true);
        localCriteria.setPowerRequirement(3);
        String str = paramLocationManager.getBestProvider(localCriteria,
                true);
        return str;
    } catch (Exception localException) {
        while (true) {
            localException.printStackTrace();
        }
    }
}

private void drawRoute(ArrayList<Location> paramArrayList,MapView paramMapView) {
    List<Overlay> overlays = paramMapView.getOverlays();
    //Changed for smooth rendering
        overlays.clear();
    for (int i = 1; i < paramArrayList.size(); i++) {
        overlays.add(new RouteOverlay(getGeoByLocation(paramArrayList.get(i - 1)), getGeoByLocation(paramArrayList.get(i)),2));
    }
}

public void startRecording() {
    this.isMeasuring = true;
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(getLocationProvider(lm),500,2,this.locationListener);
    /*if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
          gpsstatus.setText("Gps Is Enabled");
    }else
    { gpsstatus.setText("Gps Is disabled");}*/
}

1 个答案:

答案 0 :(得分:0)

您能否确认您始终使用GPS追踪路线?否则,您可能会得到不正确的结果,并且您的补丁可能会像Apple地图上那样绘制对于那些情况,总是尝试使用GPS,我确定你不希望在地图上绘制错误的路径。网络提供商可以对地图上的相对点进行三角测量,但在这种情况下99%的时间都是错误的,因为该位置是相对的。

干杯