Android LocationManager 类未返回正确的纬度/经度
我创建了一个程序,显示地图视图,其中包含手机的位置,并且能够通过电子邮件发送该位置(纬度、经度)。 (将一些位置添加到数据库中)。
我使用 LocationManager 类和 getLatitude / getLongitude 方法来获取手机的位置。我的代码如下所示:
double MyLongitude = 0;
double MyLatitude = 0;
Location location = null;
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, new ShowLocation());
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
MyLatitude = location.getLatitude();
MyLongitude = location.getLongitude();
在模拟器上测试我的应用程序时,一切正常:mapView 显示我固定的位置(使用 telnet 和 geo fix 命令),并且我可以通过电子邮件发送该位置。我在朋友的电话上得到了同样的结果;一切似乎都正常。
问题出在我的手机上。我可以在地图视图上显示我的位置(蓝色小圆圈),但是当我尝试通过电子邮件发送我的位置时,纬度和经度都设置为 0。
以下是在地图视图上显示我的位置的代码:
MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);
myLocation.enableMyLocation();
我不不明白为什么我的位置在显示时似乎有效,但我无法获得正确的纬度和经度,而它似乎在模拟器和我朋友的手机上有效。
这是由于缺少代码还是类似的原因?这是我手机的问题吗?我检查了权限,并且已经拥有所需的权限:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
我还尝试使用 LocationManager.NETWORK_PROVIDER 而不是 LocationManager.GPS_PROVIDER 进行测试,但得到了相同的结果。
I created a program that displays a mapView, with the location of the phone, and the ability to send this location (latitude, longitude) through email. (to add some places to a database).
I'm using the LocationManager class and getLatitude / getLongitude methods to get the phone's location. My code looks like this:
double MyLongitude = 0;
double MyLatitude = 0;
Location location = null;
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, new ShowLocation());
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
MyLatitude = location.getLatitude();
MyLongitude = location.getLongitude();
When test my application on the emulator, everything is OK: mapView displays the location I fixed (using telnet and geo fix command), and I can send the location through email. I get the same result on the phone of a friend; everything seems to work OK.
The problem is on my phone. I can display my location on the mapView (the little blue circle), but when I try to send my location through email, the Latitude and Longitude were both set to 0.
Here is the code to display my location on the mapView:
MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);
myLocation.enableMyLocation();
I don't understand why my location appears to work when displayed, but I can't get the correct latitude and longitude, while it seems to work on emulator and my friend's phone.
Is this due to missing code or something like that? Is this a problem with my phone? I checked the permissions, and I already have the needed ones:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
I also tried to test with LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER, but got the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以重写 MyLocationOverlay 上的 onLoationChanged(Location l) ,只要叠加层获取新位置来绘制您的位置,就会调用该方法,而不是请求位置更新和 getLastKnownLocation。
我猜测 getLastKnownLocation 返回 0, 0 因为尚未修复位置。
You can override onLoationChanged(Location l) on your MyLocationOverlay, which will get called whenever the overlay gets a new location to draw your position, instead of requesting location updates and getLastKnownLocation.
I am guessing that getLastKnownLocation is returning 0, 0 because there hasn't been a location fix yet.