これは私の位置クラスで、位置の経度と緯度を取得していますが、現在の位置を変更すると経度と緯度の値が更新されないという問題があります。
自宅の場所をデバイスに保存すると、アプリは常に同じ経度と緯度の値を取得し、現在の場所の詳細を取得しません。例: 今日ニューヨークにいて、アプリを立ち上げたところ、場所はニュージャージーの自宅でした。
public class GetLocation extends Activity {
protected LocationManager locationManager;
protected Button retrieveLocationButton;
Context mContext;
Double Longitude, Latitude;
String NetworkErrmsg = "", GpsErrmsg = "", ProviderErrmsg = "", NoNetworkmsg = "", GpsSuccesmsg = "", NetworkSuccesmsg = "";
public GetLocation(Context mContext) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void getLocationDetails() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 100, new MyLocationListener());
boolean gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gps_enabled) {
GpsErrmsg = "No Gps Connectivity";
} else {
showCurrentLocation(LocationManager.GPS_PROVIDER);
GpsSuccesmsg = "Gps Enabled";
}
if (network_enabled && !gps_enabled) {
showCurrentLocation(LocationManager.NETWORK_PROVIDER);
NetworkSuccesmsg = "Network Provider Enabled. Please Enable your GPS settings to get exact onaround you list";
}
if (!gps_enabled && !network_enabled) {
NoNetworkmsg = "Please Enable your Network/GPS settings to get onaround you list";
}
}
protected void showCurrentLocation(String provider) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Longitude = location.getLongitude
Latitude = location.getLatitude();
} else {
NoNetworkmsg = "Please Enable your Network/GPS settings to get onaround you list";
Longitude=null; Latitude=null;
}
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
System.out.println("Longitude " + location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle b) {
// Toast.makeText(GetLocation.this, "Provider status changed", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String s) {
// Toast.makeText(GetLocation.this, "Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String s) {
// Toast.makeText(GetLocation.this, "Provider enabled by the user. GPS turned on", Toast.LENGTH_LONG).show();
}
}
}