問題描述
我正在嘗試為我的新導航應用獲取用戶位置.我想經常檢查用戶的位置,它必須是準確的.我使用示例中的以下代碼來獲取位置.
i am trying to get the users location for my new navigation app. i want to check the users location frequently and it has to be accurate . I use the following code from sample to get the location .
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private String provider;
private TextView a;
private TextView b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (TextView) findViewById(R.id.a);
b = (TextView) findViewById(R.id.b);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
a.setText("Location not available");
b.setText("Location not available");
}
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider,0, 1, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
a.setText(String.valueOf(lat));
b.setText(String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
但是當我移動時,位置坐標并沒有更新,我總是得到一個恒定的值......有沒有更好的方法來獲取位置?根據 android 文檔,我可以為此使用 Google Play 位置服務或 Android 平臺位置 API.我檢查了兩者的示例代碼,它實現了 LocationListener 接口來獲取用戶位置.那么它們之間的實際區別是什么?
But location cordinates are not updating as i move,i get a constant value always ... is there any better way to get location ? According to to android docs i can use Google Play Location Service or Android platform location APIs for this . I checked the sample code for both and it implements the LocationListener interface to get the user location .So whats the actual difference between them?
推薦答案
絕對使用新的 google play 定位服務.他們在室內的工作比舊的位置經理好得多.我創建了一個使用新的 google play 位置服務并定期在后臺運行的示例.看看吧:
Absolutely use the new google play location services. They work much better indoors than the old location manager. I have created a sample that uses the new google play location services and works in the background periodically. Check it out:
https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android
這篇關于查找位置:Google Play 位置服務或 Android 平臺位置 API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!