2017-04-17 61 views
2

我有以下代碼:如何防止谷歌地圖V2 Android中的藍點更新?

private long fasterIntervalTime = 1 * 5 * 1000; 
private long intervalTime = 1 * 20 * 1000; 
private LocationRequest mLocationRequest; 

public boolean getLocation() {  
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLocationRequest.setNumUpdates(1); 
    mLocationRequest.setInterval(intervalTime); 
    mLocationRequest.setFastestInterval(fasterIntervalTime); 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.d(TAG, "Getting location : " + (location == null)); 
    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(
       mGoogleApiClient, this); 
    } 
} 

如何防止藍點確實太快了更新的谷歌地圖?

當藍點位置發生變化時,它不會調用onLocationChanged

+0

你有沒有在你的類中實現LocationListener的? – Bhavnik

+0

是的,因爲我可以獲得很長的時間並且不可能複製代碼,因爲它涉及很多類。 – Rendy

+0

好吧,沒關係,所以請嘗試從代碼中刪除setNumUpdates方法,因爲它只會給您一次性的位置更新。閱讀這個android官方文檔: - https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setNumUpdates(int) – Bhavnik

回答

0
//stop location updates 
if (mGoogleApiClient != null) { 
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
} 

刪除setNumUpdates,因爲它會調用一次