Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markers move when zooming with Google Maps Android API v2

When adding some markers on a map using Google Maps Android API v2, the markers are no set to the right positions and move when zooming.

When zooming in they get closer to the right positions, as if they were translated by a factor related to the zoom.

What's wrong?

Example zooming in:

enter image description hereenter image description hereenter image description here

fragment

public class CenterMapFragment extends Fragment {

    private GoogleMap mMap;
    private List<Center> mCenterList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.center_map_fragment, container, false);
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState){

        super.onActivityCreated(savedInstanceState); 

        mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        if (mMap != null) {

            mCenterList = MyApplication.dbHelper.getAllCenter();

            for(Center center : mCenterList){

                mMap.addMarker(new MarkerOptions().position(new LatLng(center.lat, center.lng)).icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.myicon)));

            }
        }

    }

}

layout

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
like image 712
jul Avatar asked Apr 19 '13 11:04

jul


People also ask

How do I move a marker in Google Maps API?

Just try to create the marker and set the draggable property to true . The code will be something as follows: Marker = new google.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

How do I stop my maps from moving on Android?

A user can scroll (pan) around the map by dragging the map with their finger. You can disable scrolling by calling UiSettings. setScrollGesturesEnabled(boolean).

How do I fix Google Maps zoom?

To fix Google maps zooming problems, for Google maps default zoom you want to know how to change the zoom level on Google Maps. You can change the zoom level by going to the Edit map page and then selecting 'default zoom level' in the map information section and then clicking save map.


1 Answers

For custom markers you need also use

MarkerOptions.anchor(float u, float v)

which describes where is the pointer point on the marker. By default this point is located at middle-bottom of the image.

like image 51
Dmitry Zaytsev Avatar answered Sep 28 '22 11:09

Dmitry Zaytsev