次のコードを使用して、アプリケーションのネットワークプロバイダーから現在地を取得します。
LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(network_enabled){
Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
しかし、それは私の実際の場所から約300〜700メートル離れた場所を提供します。
これは、ネットワークプロバイダーから予想されます。しかし、問題は次のとおりです。
このネットワークプロバイダーのみを有効にし、GPSを使用せずに、Foursquareアプリケーションを開いて、現在地を正確に表示します。アプリケーションに戻ると、正確な現在の場所が表示されるか、Foursquareが表示したのと同じ場所が表示されます。
ナビゲーター、マップなどのGoogleアプリでも同じことが起こります。
これはどのように行うことができますか?ネットワークプロバイダーだけに基づいて、他のアプリはどのようにして正確な場所を取得できますか?
完全なコード:
public class MyLocationActivity extends Activity implements LocationListener {
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
dumpLocation(location);
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
super.onPause();
mgr.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 15000, 10, this);
}
private void dumpLocation(Location l) {
if (l != null){
myLocationLatitude = l.getLatitude();
myLocationLongitude = l.getLongitude();
}
}
}
ありがとうございました