Android:计算到另一个精确点的距离

时间:2013-01-07 20:52:40

标签: java android google-maps gps geopoints

我创建了一种计算从当前位置到硬编码位置的距离的方法。但它不起作用。计算器没有显示距离。这有什么不对?

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

}

我的onLocationChanged方法。

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

}

1 个答案:

答案 0 :(得分:0)

从外观上看,您正在接收位置更新。 Location对象具有计算到另一个位置的距离的功能。这样你就可以检查你是否在半径范围内。