查找位置:Google Play 位置服务或 Android 平台位置 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21397177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding Location : Google Play Location Service or Android platform location APIs
提问by Fazil Uruniyengal
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();
}
}
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?
但是位置坐标不会随着我的移动而更新,我总是得到一个恒定的值......有没有更好的方法来获取位置?根据 android 文档,我可以为此使用 Google Play 位置服务或 Android 平台位置 API。我检查了两者的示例代码,它实现了 LocationListener 接口以获取用户位置。那么它们之间的实际区别是什么?
回答by nickfox
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:
绝对使用新的 google play 位置服务。他们在室内的工作比旧的位置经理好得多。我创建了一个示例,它使用新的 google play 位置服务并定期在后台工作。一探究竟:
https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android
https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android
回答by Shobhit Puri
When I was working with it, I came across 2 good sources:
当我使用它时,我遇到了两个很好的来源:
The blog post: Android Location Fused Provider. It includes a great complete tutorial on using the FusedLocationProviderApito get location in Android.
Answers on following Android LocationClient class is deprecated but used in documentationquestion.
博文:Android Location Fused Provider。它包括一个关于使用FusedLocationProviderApi在 Android 中获取位置的完整教程。
不推荐使用以下Android LocationClient 类的答案,但在文档问题中使用。
Basically both talk about the similar steps as mentioned in the Google I/O session, link to which is mentioned in one of the answers:
基本上两者都讨论了 Google I/O 会议中提到的类似步骤,其中一个答案中提到了链接:
First, to get a
GoogleClientApi
instance, like:GoogleClientApi googleApiClient = new GoogleApiClient.Builder(locationActivity) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build();
Then to call
.connect()
on it like:googleApiClient.connect();
Then in the callback
onConnected
, get the last location or even can request the location updates as per your need, eg:LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
If you want request location updates, then can use the requestLocationUpdatescall.
首先,要获取一个
GoogleClientApi
实例,例如:GoogleClientApi googleApiClient = new GoogleApiClient.Builder(locationActivity) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build();
然后
.connect()
像这样调用它:googleApiClient.connect();
然后在回调中
onConnected
,获取最后一个位置,甚至可以根据您的需要请求位置更新,例如:LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
如果你想要请求位置更新,那么可以使用requestLocationUpdates调用。
Hope it helps.
希望能帮助到你。