The_Martian
The_Martian

Reputation: 3767

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);

Upvotes: 2

Views: 853

Answers (1)

Stas Parshin
Stas Parshin

Reputation: 8293

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) {
    ...
}

Upvotes: 2

Related Questions