Android LocationManager正在发送null
我正在使用Google Map V2,我试图获取当前位置。但它总是返回null 代码Android LocationManager正在发送null,android,google-maps,google-maps-api-2,Android,Google Maps,Google Maps Api 2,我正在使用Google Map V2,我试图获取当前位置。但它总是返回null 代码 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationMa
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
每次我得到location
null。我的GPS在我的设备中工作正常。有什么问题请帮助我 根据:
如果提供程序当前已禁用,则返回null
当您使用getBestProvider(criteria,false)
时,您的意思是允许未启用的提供程序(这就是false
的意思)-如果您只想查看已启用的提供程序(这将确保getlastnownlocation不会返回null),请将其切换到true
请注意,getLastKnownLocation
可能已经过时,如果您需要获取最近的位置,您可能仍然需要查找位置更新。请尝试以下操作:
从全球范围来看:
private LocationManager location_manager;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location = null; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
从任何需要的位置调用此方法:
public Location getCurrentLatLong(Context conext,LocationListener con)
{
try
{
location_manager = (LocationManager) ((Activity) con)
.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = location_manager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = location_manager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
Toast.makeText(conext, "No provider is enabled", Toast.LENGTH_SHORT).show();
}
else
{
this.canGetLocation = true;
if (isNetworkEnabled)
{
location_manager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, con);
Log.d("Network", "Network Enabled");
if (location_manager != null)
{
location = location_manager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
location_manager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, con);
Log.d("GPS", "GPS Enabled");
if (location_manager != null)
{
location = location_manager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return location;
}
有不同的方法。首先,确保您在清单中拥有所有必需的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
使用GPS查找位置需要更多的时间,因此您会收到空值,如果您使用网络提供商,您会在第二个open native map应用程序中获得它,然后打开它提供位置的应用程序,这是GPS提供商的问题,您最好尝试融合位置提供商以避免此问题。请清除getCurrentLatLong()的参数只需在其中传递上下文,那么这个LocationListener con参数呢?只需忘记,不要在其中传递任何参数。这是我在项目中的要求。您只需将“con”替换为您的类名。这在您发现itI时的方法中得到异常使用:java.lang.IllegalArgumentException:无效提供程序:android.location.LocationManager.checkProvider(LocationManager.java:1619)02-28 13:40:07.122:E/AndroidRuntime(2852):在android.location.LocationManager.RequestLocationUpdate(LocationManager.java:427)02-28 13:40:07.122:E/AndroidRuntime(2852):在com.test.googlemap.MainActivity.onResume(MainActivity.java:129)
if(_locationManager != null)
{
if(_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null)
return new Location(_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER),true);
else if(_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null)
return new Location(_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER),true);
}