2016-06-17 5 views
0

У меня есть функция, которая перенастраивает мое текущее местоположение.Android- Текущее местоположение

public LatLng getCurrentLocation() { 

     LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String provider = service.getBestProvider(criteria, false); 

     Location location = service.getLastKnownLocation(provider); 
     LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude()); 
     //Log.e("Location",userLocation.latitude+ " x "+ userLocation.longitude); 

     return userLocation; 

    } 

Проблема в том, что когда я переехал в другое место (50 км), я все еще возвращаю предыдущее местоположение. Любые идеи, почему это так?

+0

пожалуйста, проверьте этот answear: [http://stackoverflow.com/questions/17591147/how-to- получить тока-место-в-андроида] (http://stackoverflow.com/questions/17591147/how-to-get-current-location-in-android) –

ответ

0

Используйте onLocationChanged вместо вашей функции, вы можете попробовать этот простой пример, который я использую:

@Override 
public void onLocationChanged(Location location) { 

    // TextView to show your current location in your activity 
    TextView tvLocation = (TextView) findViewById(R.id.tv_location); 

    // Getting latitude of the current location 
    double latitude = location.getLatitude(); 

    // Getting longitude of the current location 
    double longitude = location.getLongitude(); 

    // Creating a LatLng object for the current location 
    LatLng latLng = new LatLng(latitude, longitude); 

    Log.i("Current position", String.valueOf(latLng)); 

    // add marker to the current position 
    googleMap.addMarker(new MarkerOptions() 
      .position(latLng) 
      .draggable(true) 
      .icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_RED))); 

    // Showing the current location in Google Map 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng )); 

    // Zoom in the Google Map 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 

    // Setting latitude and longitude in the TextView tv_location 
    tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude); 

} 
Смежные вопросы