【问题标题】:Markers move when zooming with Google Maps Android API v2使用 Google Maps Android API v2 缩放时标记移动
【发布时间】:2013-04-19 11:12:31
【问题描述】:

使用 Google Maps Android API v2 在地图上添加一些标记时,标记未设置到正确的位置并在缩放时移动。

在放大时,它们会更接近正确的位置,就好像它们被与缩放相关的因素平移了一样。

怎么了?

放大示例:

片段

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

【问题讨论】:

  • 现在,当您添加屏幕截图时,我可以说问题肯定出在锚点
  • 你的图标上有空白吗?
  • 同样的问题有解决办法吗?
  • @UsmanGhauri 是的,已接受。
  • @jul :对我不起作用。我尝试添加锚点它仍然定位到稍微偏离。我点击并试用了这些,但仍然没有运气。我使用 svg 图标作为定位销

标签: android google-maps-markers google-maps-android-api-2


【解决方案1】:

对于自定义标记,您还需要使用

MarkerOptions.anchor(float u, float v)

它描述了标记上的指针指向的位置。默认情况下,该点位于图像的中底部。

【讨论】:

  • BitmapDescriptor 没有方法锚。它是一个 MarkerOptions 并且是:“图像上将放置在标记的 LatLng 位置的点。默认为图像底部的中间位置。”。它不应该修改位置。
  • @jul 对不起,确实 - 这是MarkerOptions 的一种方法。无论如何,这就是你要找的。 “它不应该修改位置” - 意味着图像的位置不会影响地图上的实际纬度和经度。它只是将 u*100% 上的图像向右平移,将 v*100% 上的图像向上平移。
  • @jul,如果你能提供你的问题的截图可能会有所帮助。
  • 我添加了图片。我刚刚尝试使用默认标记,它可以正常工作。
【解决方案2】:

要显示窗口信息,您必须在“mMap”上使用 setInfoWindowAdapter 或为标记设置标题。

关于图标,缩小时它似乎在移动,但锚点始终指向地图上的同一位置。如果您使用的图像不遵循 Google 的图标模式(锚点位于图像底部的中间),您可以设置锚点。为此,您必须使用MarkerOptions' method anchor(u,v)

【讨论】:

  • 正如@jul 提到的-anchorMarkerOptions 的一种方法。我只是忘记了)
猜你喜欢
  • 2014-01-15
  • 2013-02-05
  • 2014-01-12
  • 2014-01-25
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
相关资源
最近更新 更多