0

当农民在他的农场周围移动时,我正在尝试绘制路径以下是我的课程,我的问题是当我开始跟踪线路移动时,即使用户没有移动并且路径没有在他移动时更新

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);
}

主要活动:

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");}*/
}
4

1 回答 1

0

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

干杯

于 2013-02-27T09:17:57.160 回答