简体   繁体   中英

Android - Google Maps - Camera Update every location change

I'm building an app, which has to keep the user in the center of the map all the time.

I have set up the following:

locationManager = (LocationManager) saveContext
        .getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MapLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,
        10, locationListener);
if (myLocation != null) {
    setLocation(myLocation);
}

private void setLocation(Location location) {
    workoutMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            location.getLatitude(), location.getLongitude()), 18.0f));
}

This works, as long I have a lastKnownLocation in my phone's history. The map will center correctly.

I wish that every time I'm getting a new location update, setLocation() will be executed again, with the new location info.

I've tried creating a thread which just does get the lastKnownLocation and then setLocation(); But this isn't working out.

I suggest you take a look at the excellent and complete sample at Googles Receiving Location Updates guide.

It includes sample code and an Android project you can import in ADT straight away.

The callback method that Location Services invokes to send a location update to your app is specified in the LocationListener interface, in the method onLocationChanged().

There you can put your camera update.

Just in case anyone else stumbles on this, it can be done and the answer, given by user Rob, is in another StackOverflow post here .

The basic idea is to create a CameraPosition object, pass this to the CameraUpdateFactory and then have the map animate the CameraUpdate object.

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