问题描述:

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

}

网友答案:

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.

相关阅读:
Top