Android-当前位置

时间:2016-06-17 09:42:12

标签: android location

我有功能可以重新调整我当前的位置。

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公里)时,我仍然会返回上一个位置。任何想法为什么会这样?

1 个答案:

答案 0 :(得分: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 );

}