2013-02-27 44 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