0

i am trying to draw the path as the farmer moves around his farm the below are my classes ,my problem is when i start tracing the line moves even when the user is not moving and path is not updating as he moves

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");}*/
}
user2114562
  • 21
  • 1
  • 1
  • 5

1 Answers1

0

Can you confirm that you're always using the GPS to trace the route? Otherwise you might get incorrect results and your patch might get drawn like on Apple maps. For those cases always try using the GPS, I'm sure you don't want incorrect path to be drawn on the map. The network provider can triangulate a relative point on the map, but it will be incorrect 99% of the time in this case, because the position is relative.

Cheers

g00dy
  • 6,752
  • 2
  • 30
  • 43
  • i am not sure how to check if the returned value is from gps – user2114562 Feb 27 '13 at 09:25
  • Well, try using `GPS_PROVIDER` ONLY in your code. I think it will suit your needs the best and avoid using `NETWORK_PROVIDER`. This will give you the correct location all the time, but consider this: If you're drawing the route on a very small street, even the GPS can not guaratee you that the coordinates returned are in the middle of the street. You should also consider an algorithm, to detect the nearby street (maybe vector comparison) and color inside the street. Am I on the right track or is it something else you want to achieve? – g00dy Feb 27 '13 at 09:37
  • If you still don't know how to do this, here's a perfect (working) example, provided by @Fedor: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655 . You can implement the class, but remove the `NETWORK_PROVIDER` part, because you don't need it. – g00dy Feb 27 '13 at 09:58