Android中如何获取手机的经度、纬度?
我在网上阅读了一些教程,发现LocationManager就是这样做的。 这是我在网上找到的代码。但是,我不明白。为什么我不能获取经度/纬度?为什么要经历所有“更改”位置的事情......
lm=(LocationManager)this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 10, new LocationListener(){
@Override
public void onLocationChanged(Location location) {
Log.d("a","onLocationChanged: lat="+location.getLatitude());
Log.d("a","onLocationChanged: lat="+location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("a","onProviderDisabled: " +provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.d("a","onProviderEnabled: " +provider);
}
@Override
public void onStatusChanged(String provider, int status,Bundle extras) {
Log.d("a","onStatusChanged: " +provider + status);
}
});
顺便说一句,这段代码不起作用。当我在计时器中运行它时......什么也没有发生。没有记录任何内容。我只想要经纬度!!
I read some tutorials online, and found that LocationManager does this.
This is the code I found online. However, I don't get it. Why can't I just get the long/lat? Why go through all the "change" location things...
lm=(LocationManager)this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 10, new LocationListener(){
@Override
public void onLocationChanged(Location location) {
Log.d("a","onLocationChanged: lat="+location.getLatitude());
Log.d("a","onLocationChanged: lat="+location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("a","onProviderDisabled: " +provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.d("a","onProviderEnabled: " +provider);
}
@Override
public void onStatusChanged(String provider, int status,Bundle extras) {
Log.d("a","onStatusChanged: " +provider + status);
}
});
By the way, this code doesn't work. When I run it in a timer...nothing happens. Nothing gets logged. I just want the longitude latitude!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LocationManager 可以返回最后一个已知位置,但这不是很精确,因此您必须请求位置更新并等待第一个位置更新。然后您可以继续接收它们或取消它们。您可以请求网络和 GPS 更新。
如果您使用手机来测试代码,那么您必须等待 GPS 修复,这可能需要一段时间。网络提供商应该更快。
如果您使用 Eclipse,那么您必须使用 < a href="http://en.wikipedia.org/wiki/Defense_Discovery_Metadata_Specification" rel="nofollow noreferrer">DDMS。您无法从模拟器测试网络提供商(您永远不会收到更新)。
LocationManager can return a last known location but this is not very precise so you have to request location updates and wait for the first one. Then you can continue receiving them or cancel them. You can request both network and GPS updates.
If you are using a phone to test your code then you have to wait for a GPS fix, and this can take a while. Network provider should be much faster.
If you are using Eclipse, then you have to send artificial GPS updates using DDMS. You can't test a network provider from the emulator (you will never receive an update).