2013-12-13 51 views
0

我正在開發地理位置應用程序,我需要計算用戶速度,以便獲取用戶速度我在每30秒使用LocationManager獲取當前位置,爲此我使用了以下代碼。如何在Android中設置最大時間到LocationManager?

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*30, 0, myLocationListener); //from NETWORK_PROVIDER 

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*30, 0, myLocationListener); //from GPS_PROVIDER 

但我的問題是我想要基於固定時間間隔的位置在這裏是30秒。現在我每45秒鐘後得到的位置也有所不同。所以,如果有任何知道如何將'maxTime'設置爲位置經理,請讓我知道。

此外,如果任何人知道位置偵聽器調用基於時間/距離的業務邏輯也請讓我知道。

謝謝先進。

回答

0

使用新加入熔融的位置提供API

import android.app.IntentService; 
import android.content.Intent; 
import android.location.Location; 
import android.os.Bundle; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 

public class Locations extends IntentService implements 
    GooglePlayServicesClient.ConnectionCallbacks, 
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { 


public Locations() { 
    super("Locations"); 
    Log.d("Locations", "Location service started... "); 
} 

private LocationRequest locationRequest; 
private LocationClient locationClient; 
private Location location; 
private static final int INTERVAL = 1800000; 
private static final int FASTEST_INTERVAL = 60000; 

@Override 
protected void onHandleIntent(Intent intent) { 
    locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    locationRequest.setInterval(INTERVAL); 
    locationRequest.setFastestInterval(FASTEST_INTERVAL); 
    locationClient = new LocationClient(this, this, this); 
    locationClient.connect(); 
} 

@Override 
public void onLocationChanged(Location l) { 
// do something on Location change. 
} 

@Override 
public void onConnectionFailed(ConnectionResult arg0) { 

} 

@Override 
public void onConnected(Bundle arg0) { 
    Log.w("Locations", "Location client connected..."); 

     // get last location 
    location = locationClient.getLastLocation(); 
    Log.w("Locations", "Latitude : "+location.getLatitude() + ""); 
    Log.w("Locations", "Longitude : "+location.getLongitude() + ""); 
} 

@Override 
public void onDestroy() { 
    if (locationClient.isConnected()) { 
     locationClient.removeLocationUpdates(this); 
    } 
    locationClient.disconnect(); 
} 

@Override 
public void onDisconnected() { 
} 

} 

來源https://developer.android.com/training/location/index.html

+0

但就是這個API能夠在NETWORK_PROVIDER工作,我的意思是,如果GPS關閉和NETWORK_PROVIDER爲ON,則是GooglePlayServicesClient能夠給我的位置。 –

+0

@AmolWadekar是的,它會給你,你只是改變了優先順序。 –

相關問題