简体   繁体   中英

Simple tracking app using Google Maps on Android

I'm developing a simple app showing the position of the user on a map. When he moves, the camera follows him to keep the user at the center of the map. Everything seems very simple, here is the relevant code.

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    LocationManager locationManager = null;
    Location currentBestLocation = null;
    LocationListener locationListener = null;
    GoogleMap map = null;
    Marker marker = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        currentBestLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));

        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {

                  currentBestLocation = location;
                  if(map != null){
                    LatLng position = new LatLng(location.getLatitude(), location.getLongitude());

                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 17));
                    if(marker != null) marker.remove();
                    marker = map.addMarker(new MarkerOptions()
                            .position(position)));
                  }


            }
          .
          .
          .
          };


    }

    @Override
    public void onMapReady(GoogleMap map) {
        this.map = map;

        this.map.setMyLocationEnabled(true);
        if(currentBestLocation != null)
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentBestLocation.getLatitude(), currentBestLocation.getLongitude()), 17));
    }

    @Override
    protected void onStart() {
        super.onStart();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);   
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationManager.removeUpdates(locationListener);
    }
}

However with this implementation I'm facing the following issue: although the camera follows the user movements, very often the street name layer fails to update or there's a fuzzy/blurry section that fails to update until I do a manual scroll of the map. Then the entire map updates instantly. What am I doing wrong?

In your onLocationChanged use this code.

 CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
    map.moveCamera(center);
    map.animateCamera(zoom);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM