0

I have created a method to calculate the distance from my currentposition to my hardcoded position. But it does not work. The calculator are not showing the distance. What is wrong here?

public void ourLocation() 

{

    myLocation = new MyLocationOverlay(this, map);
    map.getOverlays().add(myLocation);
    myLocation.enableMyLocation();

    // Find my position
    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            20000, 0, this);

    controller = map.getController();

}

My onLocationChanged method.

public void onLocationChanged(Location location1) {
        // Updating location and zoom
        controller.setCenter(new GeoPoint(
                (int) (location.getLatitude() * 1000000), (int) (location
                        .getLongitude() * 1000000)));
        controller.setZoom(15);

    Criteria crit = new Criteria();
    towers = locationManager.getBestProvider(crit, false);

    location = locationManager.getLastKnownLocation(towers);

    //Trying to get MyPostion.
    Location myLocation = new Location("point A");

    myLocation.setLongitude(location.getLongitude());
    myLocation.setLatitude(location.getLatitude());

    Location locationB = new Location("point B");

    locationB.setLatitude(55777816);
    locationB.setLongitude(12533358);

    float distance = myLocation.distanceTo(locationB);

    //Showing the distance
        AlertDialog alert = new AlertDialog.Builder(GoogleMaps.this)
                .create();
        alert.setTitle((int)distance);

}
Zaz
  • 1,074
  • 3
  • 17
  • 29

1 Answers1

0

By the looks of it you are receiving location updates. The Location object has functionality to calculate the distance to another location. That way you can check if you are within the radius.

Lieuwe
  • 1,734
  • 2
  • 27
  • 41
  • What is the name of the functionality? Or can you provide an example? – Zaz Jan 07 '13 at 21:27
  • Location.distanceTo(Location dest) – Lieuwe Jan 07 '13 at 21:52
  • Okay. How do i combine this code: compass.enableMyLocation(); lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, this); to be the locationA? – Zaz Jan 07 '13 at 22:53
  • i just updated my code ^^ .. I did as you said, but it is like i am not getting my last known position? – Zaz Jan 08 '13 at 10:03