2016-03-24 16 views
0

私のAndroid Appに問題があります。Android LocationManagerは常に同じ古い位置を返します

のonCreate:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    requestPermission(); 
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, this); 
     // Define the criteria how to select the location provider -> use 
     // default 
     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     if (location != null) { 
      onLocationChanged(location); 
     } 
    } 

} 

onLocationChanged:

@Override 
public void onLocationChanged(Location location) { 

    if (mMap != null) { 
     setMoveToLocation(location, mMap.getCameraPosition().zoom); 
    } 

} 

そして

private void setMoveToLocation(Location location, float zoom) { 
    // Add a marker at given location and move the camera 
    int lat = (int) (location.getLatitude()); 
    int lng = (int) (location.getLongitude()); 
    Log.i("Location", "Lat: " + lat + ", Long: " + lng + " Time: " + location.getTime()); 
    LatLng pos = new LatLng(lat, lng); 
    mMap.addMarker(new MarkerOptions().position(pos).title("Your position")); 
    Log.i("Zoom", "Zoom: " + zoom); 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, zoom)); 
} 

は出力を参照してくださいsetMoveToLocation:私は、コードの次の場所の更新を受信したいと書いた Log output

いつも同じ位置が間違っていますが、私は51,7に位置していません! GPSは間違いなくオンになっており、関連する通知やGoogleマップなどの他のアプリが表示され、正しく動作しています。 私は、Android 6.0でHuawei Honor 7を使用しています。

アイデア?あなたはintに緯度と経度をキャストしている事前に

おかげで、 Plebo

答えて

1

int lat = (int) (location.getLatitude()); 
int lng = (int) (location.getLongitude()); 

と緯度と経度ので、ダブル彼らは51と7にキャストされ、それぞれ

+0

ああ男、 どうもありがとうございました!!私は本当にあまりにも馬鹿だ:D – Plebo

関連する問題