9

好的,默认情况下,单击标记时,标记位于地图的中心。问题是,我有很高的 infoView,从布局中膨胀,它显示在地图边界之外。我能做什么,将针水平居中,并将其放置在地图中心下方,以使 infoWindow 可见。

4

4 回答 4

20

所以我最终得到了这个解决方案,它就像一个魅力。我在动画后打开 infoWindow。

int zoom = (int)map.getCameraPosition().zoom
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new 
LatLng(arg0.getPosition().latitude + (double)90/Math.pow(2, zoom), 
arg0.getPosition().longitude), zoom);
map.animateCamera(cu,500,null);
于 2013-04-19T11:53:19.877 回答
10

GoogleMaps 使用Web Mercator,它极大地扭曲了高纬度地区的数据,因为它本质上将我们的星球映射到一个正方形。多亏了这一点,您离赤道越远,您在一些简化计算中引入的误差就越大。

相反,我们将让 Android 为我们处理这些;)

在以下文本mGoogleMap中是您的GoogleMap. 我们将设置一个 onMarkerClickListener在其中打开的InfoWindow (如在 onMarkerClickListener 的默认实现中),然后根据GoogleMap 投影重新计算相机中心位置,而不是以标记本身为中心。

mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            // Calculate required horizontal shift for current screen density
            final int dX = getResources().getDimensionPixelSize(R.dimen.map_dx);
            // Calculate required vertical shift for current screen density
            final int dY = getResources().getDimensionPixelSize(R.dimen.map_dy);
            final Projection projection = mGoogleMap.getProjection();
            final Point markerPoint = projection.toScreenLocation(
                    marker.getPosition()
            );
            // Shift the point we will use to center the map
            markerPoint.offset(dX, dY);
            final LatLng newLatLng = projection.fromScreenLocation(markerPoint);
            // Buttery smooth camera swoop :)
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(newLatLng));
            // Show the info window (as the overloaded method would)
            marker.showInfoWindow();
            return true; // Consume the event since it was dealt with
        }
    });

animateCamera()将相机平滑地移动到计算的位置,您可以使用 跳转到新位置moveCamera()

我们还需要将两个维度map_dxmap_dy, 添加到资源中:

<resources>

    <!-- Where to shift the map center when user taps on marker -->
    <dimen name="map_dx">10dp</dimen>
    <dimen name="map_dy">-100dp</dimen>

</resources>

我们已经完成了,这种方法(与其他方法不同)在整个地图上都能很好地工作,甚至靠近地球两极。

于 2015-04-02T16:13:09.230 回答
1

在 infoWindow 的适配器中试试这个:

@Override
public View getInfoWindow(Marker marker) {

View infoWindowLayout = (View) inflater.inflate(R.layout.my_info_window_layout, null);

int width = display.getWidth() (I put here a "*2/3" too to not fill the whole screen, but this is your choice);

LinearLayout infoWindowSubLayout = (LinearLayout) infoWindowLayout.findViewById(R.id.myInfoWindowSubLayout);

android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(width, LayoutParams.MATCH_PARENT);

infoWindowSubLayout.setLayoutParams(lp);

return infoWindowLayout;
}

在我的例子中,这个布局文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myInfoWindowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/..."
android:orientation="horizontal"
... >

<LinearLayout
    android:id="@+id/myInfoWindowSubLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    ...>

    <LinearLayout
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        CONTENT HERE
    </LinearLayout>

   CONTENT HERE TOO
</LinearLayout>

</LinearLayout>

好吧,这对我有用,请随时重写

于 2013-04-19T10:23:21.230 回答
1

我尝试了以下完美的解决方案

mMap.setOnMarkerClickListener(new OnMarkerClickListener() 
        {
            @Override
            public boolean onMarkerClick(Marker marker)
            {
                int yMatrix = 200, xMatrix =40;

                DisplayMetrics metrics1 = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics1);
                switch(metrics1.densityDpi)
                {
                case DisplayMetrics.DENSITY_LOW:
                    yMatrix = 80;
                    xMatrix = 20;
                    break;
                case DisplayMetrics.DENSITY_MEDIUM:
                    yMatrix = 100;
                    xMatrix = 25;
                    break;
                case DisplayMetrics.DENSITY_HIGH:
                    yMatrix = 150;
                    xMatrix = 30;
                    break;
                case DisplayMetrics.DENSITY_XHIGH:
                    yMatrix = 200;
                    xMatrix = 40;
                    break;
                case DisplayMetrics.DENSITY_XXHIGH:
                    yMatrix = 200;
                    xMatrix = 50;
                    break;
                }

                Projection projection = mMap.getProjection();
                LatLng latLng = marker.getPosition();
                Point point = projection.toScreenLocation(latLng);
                Point point2 = new Point(point.x+xMatrix,point.y-yMatrix);

                LatLng point3 = projection.fromScreenLocation(point2);
                CameraUpdate zoom1 = CameraUpdateFactory.newLatLng(point3);
                mMap.animateCamera(zoom1);
                marker.showInfoWindow();
                return true;
            }
        });
于 2013-10-14T07:32:21.753 回答