我正在使用谷歌地图V2,我试图获取当前位置。但它总是返回null。
代码
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
每次我收到location
null。我的GPS在我的设备上工作正常可以帮助我。
答案 0 :(得分:0)
根据getLastKnownLocation documentation:
如果当前禁用了提供程序,则返回null。
当您使用getBestProvider(criteria, false)
时,您说您允许未启用的提供程序(false
表示的意思) - 如果您只想查看已启用的提供程序,请将其切换为true
(这将确保getLastKnownLocation不返回null)。
请注意,getLastKnownLocation
可能已过期,如果您需要获取最近的位置,您可能仍希望查找位置更新。
答案 1 :(得分:0)
试试这个:
全球范围内:
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;
}
答案 2 :(得分:0)
有不同的方法。首先,请确保您的清单中包含所有必需的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
如果您只想要一个大概的位置,您可以获得最后一个位置(GPS或网络)
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);
}