Finding Location : Google Play Location Service or Android platform location APIs(查找位置:Google Play 位置服务或 Android 平台位置 API)
问题描述
我正在尝试为我的新导航应用获取用户位置.我想经常检查用户的位置,它必须是准确的.我使用示例中的以下代码来获取位置.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:查找位置:Google Play 位置服务或 Android 平台位置
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01