2017-05-01 37 views
0

我想要計算您當前位置和三個標記之間的距離。 一切工作正常,但如果我改變我的位置的距離等於前的距離,我沒有得到新的座標。這裏是我的代碼:如何獲得總是最新的當前座標

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Location current = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true)); 
    latng = String.valueOf(current.getLatitude()); 
    lngon = String.valueOf(current.getLongitude()); 
    Location club = new Location("clubloc"); 
    club.setLatitude(Double.parseDouble(marker_data.get("lat"))); 
    club.setLongitude(Double.parseDouble(marker_data.get("lng"))); 
    double distance = current.distanceTo(club); 
    distance = distance/1000; 
    distance = Math.round(distance * 100); 
    distance = distance/100; 
    TextView dis = (TextView) findViewById(R.id.distance); 
    dis.setText(String.valueOf(distance) + "KM"); 
    } 

所以我的問題是我怎麼可以更新我的座標,所以我總是得到正確的距離是多少?

回答

0

您總是得到用戶的最後一個已知位置(使用getLastKnownLocation)。你不主動要求位置更新。您可以閱讀關於here的更多信息。

+1

感謝兄弟我解決了它使用onLocationChanged方法 –