简体   繁体   中英

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:

在此输入图像描述在此输入图像描述在此输入图像描述

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

            }
        }

    }

}

<?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" />

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.

To show an window info, you have to use setInfoWindowAdapter on your "mMap" or have a title set for your marker.

About the icon, it seems to be moving when you zoom out, but the anchor always point to the same place on the map. You can set an anchor if the image you're using don't follow Google's icon pattern (anchor on the middle of the botton of the image). For that you have to do use MarkerOptions' method anchor(u,v)

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