Android 安卓:GPS定位不工作
我正在尝试检索位置,如下所示:Android 安卓:GPS定位不工作,android,gps,geolocation,Android,Gps,Geolocation,我正在尝试检索位置,如下所示: LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); // Getting the name of the provider that meets the criteria String prov
LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
String provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// NOTE: the location below is null !!
Location location = locationManager.getLastKnownLocation(provider);
String lat = location.getLatitude();
String lng = location.getLongitude();
}
上面的“位置”为空。为什么?但当我打开(谷歌)地图应用程序时,它会正确显示位置,甚至会出现一个通知图标(看起来像感叹号) 还有一个GPS坐标应用程序,它也显示为空白
我已打开手机上的设置>“位置”。如果重要的话,这是安卓4.4.4索尼Experia手机。好吧,看来你没有正确检查GPS是否正常工作。您应该采取的方式:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//GPS is not enabled !!
}
您还可以创建AlertDialog,以便用户知道GPS未启用,您可以通过以下操作将其发送到GPS设置:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
在此之后,我将实现一个位置侦听器并获取坐标,下面是一个示例,请查看
更新:他们可能是通过其他提供商(通常是当时工作更好的提供商)获取坐标的。因此,如果GPS不工作,请尝试其他提供商,以下是一个示例:
Location lastKnownLocation = null;
List<String> providers = null;
if(locationManager != null) providers = locationManager.getAllProviders();
if(providers != null)
{
for(int i=0; i<providers.size(); i++)
{
if(locationManager != null) lastKnownLocation = locationManager.getLastKnownLocation(providers.get(i));
if(lastKnownLocation != null)
{
position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
break;
}
}
}
Location lastnownlocation=null;
列表提供者=空;
if(locationManager!=null)providers=locationManager.getAllProviders();
如果(提供程序!=null)
{
对于(int i=0;iFrancesco>谢谢..但是使用Google Play服务比使用Android位置服务更好吗(我现在的做法)?那么…Google地图和其他一些位置相关的应用程序在同一部手机上如何工作?