2013-02-27 41 views
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); 
} 

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

回答

0

你能確認你總是使用GPS跟蹤的路線?否則,您可能會得到不正確的結果,並且您的修補程序可能會在Apple地圖上繪製。對於那些情況總是嘗試使用GPS,我相信你不希望在地圖上繪製不正確的路徑。網絡提供商可以對地圖上的相對點進行三角測量,但在這種情況下,99%的時間是不正確的,因爲位置是相對的。

乾杯

+0

我不知道如何檢查返回值是從GPS – user2114562 2013-02-27 09:25:45

+0

好,只嘗試在代碼中使用'GPS_PROVIDER'。我認爲它會最適合您的需求,並避免使用「NETWORK_PROVIDER」。這會一直給你提供正確的位置,但考慮一下:如果你在一條非常小的街道上繪製路線,即使GPS也無法保證你返回的座標位於街道中央。您還應該考慮一種算法,以檢測街道附近的街道(可能是矢量比較)和顏色。我在正確的軌道上,還是你想實現的其他目標? – g00dy 2013-02-27 09:37:11

+0

是的謝謝你,我只使用gps_provider – user2114562 2013-02-27 09:40:02