2013-01-07 83 views
0

我創建了一種方法來計算從我的當前位置到我的硬編碼位置的距離。但它不起作用。計算器沒有顯示距離。這裏有什麼問題?Android:計算到另一個精確位置的距離

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

} 

回答

0

從外觀上看,您正在接收位置更新。 Location對象具有計算到另一個位置的距離的功能。這樣你可以檢查你是否在半徑範圍內。

+0

功能的名稱是什麼?或者你能提供一個例子嗎? – Zaz

+0

Location.distanceTo(Location dest) – Lieuwe

+0

好的。我如何結合這段代碼: compass.enableMyLocation(); lm =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000,0,this); 要成爲位置A? – Zaz