简体   繁体   中英

Android zoom in and center around custom marker in a map

I am using Google map API V2. I have disabled the map.isMyLocationEnabled() so I use my own custom icon in place of the blue dot. Since the my location layer is disabled the map doesn't zoom in around my icon. Can someone point me on how to do that?

GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
                @Override
                public void onMyLocationChange (Location location) {
                   LatLng loc = new LatLng (location.getLatitude(), location.getLongitude());

                   if (marker != null) {
                        marker.remove(); // remove the old marker
                    }
                    marker = map.addMarker(new MarkerOptions()
                            .position(loc)
                            .draggable(true)
                            .title("Not your location? wait 10 few seconds until the icon displays your real location")
                            .visible(true)
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.mmyicon))
                            .flat(true));   // allows it rotate with the earth and change
                                            // perspective as needed and retain it's
                                            // size
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 15));
                //startIntentService(location);
                }
            };
            map.setOnMyLocationChangeListener(myLocationChangeListener);

After you disable myLocation layer by map.setMyLocationEnabled(false) , this listener become not working too.

So you should receive location by another way. https://developer.android.com/training/location/receive-location-updates.html

After you set everything correct you can draw marker on this callback.

@Override
public void onLocationChanged(Location location) {
    ...
}

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