Android 第一次运行应用程序时获得纬度和经度为零,但10分钟后,它将获得正确的值。如何解决这个问题?
我第一次得到:Android 第一次运行应用程序时获得纬度和经度为零,但10分钟后,它将获得正确的值。如何解决这个问题?,android,gps,Android,Gps,我第一次得到: latitude:0.00 longitude:0.00 address: not found latitude:13.00666178 longitude:80.25727619 address:some address 10分钟后,我得到: latitude:0.00 longitude:0.00 address: not found latitude:13.00666178 longitude:80.25727619 address:some address 也添加
latitude:0.00
longitude:0.00
address: not found
latitude:13.00666178
longitude:80.25727619
address:some address
10分钟后,我得到:
latitude:0.00
longitude:0.00
address: not found
latitude:13.00666178
longitude:80.25727619
address:some address
也添加适当的权限这是我的代码。工作非常好,并立即给出结果
LocationManager locationManager;
Location location;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
location = googleMap.getMyLocation();
if (location != null) {
LatLng latLang = new LatLng(location.getLatitude(),location.getLongitude());
cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLang, 17);
googleMap.animateCamera(cameraUpdate);
}
}
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // 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
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.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_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
首先你的问题是什么?如果可以的话,别忘了使用Wifi进行本地化,如果你在室内测试时没有Wifi,我猜可能需要很长时间,请使用线程并检查lat和lon。如果为零,请再次尝试查找位置。如果不为零,则只计算下一行。