没有得到位置
Not getting the location
我正在制作一个应用程序,其中我必须显示 1/2 的启动画面 sec.While 启动画面正在加载我必须让用户 location.location 准确性不是 concern.So 为此我写了一个代码,但我总是从位置得到空值。
代码
private Context context;
private LocationManager locationManager;
Location location;
double latitude;
double longitude;
public GetLocation(Context context) {
this.context = context;
getLocation();
}
private Location getLocation() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(getProviderName());
if (location == null) {
locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
}
return location;
}
String getProviderName() {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW); // Chose your desired power consumption level.
criteria.setAccuracy(Criteria.ACCURACY_FINE); // Choose your accuracy requirement.
criteria.setSpeedRequired(true); // Chose if speed for first location fix is required.
criteria.setAltitudeRequired(false); // Choose if you use altitude.
criteria.setBearingRequired(false); // Choose if you use bearing.
criteria.setCostAllowed(false); // Choose if this provider can waste money :-)
// Provide your criteria and flag enabledOnly that tells
// LocationManager only to return active providers.
return locationManager.getBestProvider(criteria, true);
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(context,""+location,Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(context,provider,Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(context,provider,Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(context,provider,Toast.LENGTH_LONG).show();
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(this);
}
}
/**
* Function to get latitude
*/
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
用法
private void getLocation() {
getLocation = new GetLocation(contextActivity);
latitude = getLocation.getLatitude();
longitude = getLocation.getLongitude();
if (latitude != 0 && longitude != 0) {
getLocation.stopUsingGPS();
}
}
请帮帮我。
谢谢
getLastKnownLocation
returns 如果尚未获取位置,则为 null。如果您的设备是这种情况,则以下代码段
if (location == null) {
locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
}
阻止您注册 LocationListener
,为您的位置管理器回调。
我猜你没有在 onLocationChanged 中更新位置。您是否在吐司文本中获取位置非空对象?
我的答案是用 onLocationChange 事件收到的位置更新 location 成员变量。
我正在制作一个应用程序,其中我必须显示 1/2 的启动画面 sec.While 启动画面正在加载我必须让用户 location.location 准确性不是 concern.So 为此我写了一个代码,但我总是从位置得到空值。
代码
private Context context;
private LocationManager locationManager;
Location location;
double latitude;
double longitude;
public GetLocation(Context context) {
this.context = context;
getLocation();
}
private Location getLocation() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(getProviderName());
if (location == null) {
locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
}
return location;
}
String getProviderName() {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW); // Chose your desired power consumption level.
criteria.setAccuracy(Criteria.ACCURACY_FINE); // Choose your accuracy requirement.
criteria.setSpeedRequired(true); // Chose if speed for first location fix is required.
criteria.setAltitudeRequired(false); // Choose if you use altitude.
criteria.setBearingRequired(false); // Choose if you use bearing.
criteria.setCostAllowed(false); // Choose if this provider can waste money :-)
// Provide your criteria and flag enabledOnly that tells
// LocationManager only to return active providers.
return locationManager.getBestProvider(criteria, true);
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(context,""+location,Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(context,provider,Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(context,provider,Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(context,provider,Toast.LENGTH_LONG).show();
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(this);
}
}
/**
* Function to get latitude
*/
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
用法
private void getLocation() {
getLocation = new GetLocation(contextActivity);
latitude = getLocation.getLatitude();
longitude = getLocation.getLongitude();
if (latitude != 0 && longitude != 0) {
getLocation.stopUsingGPS();
}
}
请帮帮我。 谢谢
getLastKnownLocation
returns 如果尚未获取位置,则为 null。如果您的设备是这种情况,则以下代码段
if (location == null) {
locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
}
阻止您注册 LocationListener
,为您的位置管理器回调。
我猜你没有在 onLocationChanged 中更新位置。您是否在吐司文本中获取位置非空对象?
我的答案是用 onLocationChange 事件收到的位置更新 location 成员变量。