如何在 Android (2.2) 设备上获取用户的 GPS 位置
我试图简单地获取用户的 GPS 位置,但在尝试获取设备的纬度和经度时我的应用程序崩溃了。请参阅代码以了解错误发生的位置。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
final double longitude = location.getLongitude(); //error occurs here
final double latitude = location.getLatitude(); //and here
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//longitude = location.getLongitude();
//latitude = location.getLatitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//update location via GPS PROVIDER
//can also use LocationManager.NETWORK_PROVIDER
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
我还在清单文件中包含了 ACCESS_FINE_LOCATION 权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
所以我不太确定为什么这不起作用。任何帮助将不胜感激!
I am attempting to simply acquire the user's GPS location, but my application crashes when trying to get the latitude and longitude of the device. See code for where the error occurs..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
final double longitude = location.getLongitude(); //error occurs here
final double latitude = location.getLatitude(); //and here
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//longitude = location.getLongitude();
//latitude = location.getLatitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//update location via GPS PROVIDER
//can also use LocationManager.NETWORK_PROVIDER
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
I am also including the ACCESS_FINE_LOCATION permission in the manifest file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
so I'm not quite sure why this does not work. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可以检查这个StackOverflow问题: 在 Android 上获取用户当前位置的最简单、最可靠的方法是什么?
I think you can check this StackOverflow Question: What is the simplest and most robust way to get the user's current location on Android?
您的错误很可能位于: lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
这是因为 getLastKnownLocation 是非阻塞调用,并且假设应用程序刚刚启动,系统没有“最后已知位置”。您可能想要循环,直到 getLastKnownLocation 没有获得非空值为止。
Your error is most probably at: lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
It is because the getLastKnownLocation is a non blocking call and assuming the application has just started there is no 'last known location' that the system has. You might want to loop till you dont get a non-null value for getLastKnownLocation.