[英]GPS_PROVIDER Error in Android Application
I have created an Android app to retrieve the location. 我创建了一个Android应用来检索位置。 I tried to use GPS_PROVIDER, it returned null.. But works smooth when i use NETWORK_PROVIDER.. Can any one help me fixing this?
我尝试使用GPS_PROVIDER,它返回null ..但是当我使用NETWORK_PROVIDER时工作顺利..任何人都可以帮我解决这个问题吗?
Here is the code 这是代码
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
位置location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
The location turns null when i replace "NETWORK_PROVIDER"
by "GPS_PROVIDER"
.. 当我用
"GPS_PROVIDER"
替换"NETWORK_PROVIDER"
时,该位置变为null。
I have added the following three permissions in manifest 我在清单中添加了以下三个权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
There can be two reasons why your GPS location is null GPS位置为空可能有两个原因
setting menu => Location & Security => Check Use GPS Satellites
. setting menu => Location & Security => Check Use GPS Satellites
激活您的GPS setting menu => Location & Security => Check Use GPS Satellites
。 As per documentation 根据文件
If the provider is currently disabled, null is returned.
如果当前禁用了提供程序,则返回null。
Ensure your GPS is active on the device so you don't receive null. 确保您的GPS在设备上处于活动状态,因此您不会收到空值。
Edit: 编辑:
Also try running requestLocationUpdates()
with GPS_PROVIDER
or maybe twice, with NETWORK_PROVIDER
as well. 还可以尝试使用
GPS_PROVIDER
运行requestLocationUpdates()
,也可以使用NETWORK_PROVIDER
运行两次。 The LocationManager
retrieves the location the fastest way and it stops searching. LocationManager
以最快的方式检索位置并停止搜索。 Since GPS location is retrieved slower than the network location, is stops right after if finds it therefore returning null for the GPS. 由于GPS位置的检索速度比网络位置慢,因此如果发现GPS位置因此返回null,则会立即停止。
The location turns null when i replace "NETWORK_PROVIDER" by "GPS_PROVIDER"
当我用“GPS_PROVIDER”替换“NETWORK_PROVIDER”时,该位置变为null
To solve this issue be sure to have enabled the Location services : 要解决此问题,请务必启用位置服务 :
remember the docs: 记住文档:
If the provider is currently disabled, null is returned.
如果当前禁用了提供程序,则返回null。
As an option you can use both providers if GPS is disabled then check network provider: 如果禁用GPS,您可以使用两个提供商,然后检查网络提供商:
...
myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(myLocation == null) {
myLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(myLocation != null) {
//Get values for latitude and longitude!
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.