安卓地图API V2设置自定义信息窗口位置

3
我正在为地图上的所有标记使用InfoWindowAdapter (Api v2)。 所有标记都很清晰可见。
问题是我自定义的InfoWindow大小约为500px * 300px。 当我触摸地图上的任何点时,它被设置为屏幕的中心,因此信息窗口从顶部被裁剪。 我的要求是根据信息窗口大小自动调整。
请看以下截图。

mapView.setInfoWindowAdapter(Object for InfoWindowAdapter);.. 我认为在这里没有使用对话框的方法。 - Vishal Khakhkhar
在标记点击事件中,你可以这样做... - Dhaval Parmar
请问您能否提供更多的建议或代码? - Vishal Khakhkhar
https://developers.google.com/maps/documentation/android/marker#marker_click_events - Dhaval Parmar
这里的链接 https://dev59.com/5mQn5IYBdhLWcg3wiHfl 解决了我的问题。 - Vishal Khakhkhar
4个回答

5

覆盖默认的OnMarkerClickListener行为。

  • 调用marker.showInfoWindow()
  • 使用Projection和标记位置计算屏幕应该居中的位置,并将相机动画到该位置
  • 返回true

这个答案应该可以帮助你完成第二个点:https://dev59.com/5mQn5IYBdhLWcg3wiHfl#16764140


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

-1

请查看下面的代码:我使用了简单的AlertDialog,您可以使用自定义或任何您想要的。

enter image description here

enter image description here

AbstractMapActivity.java

public class testclassmaps extends AbstractMapActivity {

    private GoogleMap map;
    private TextView text;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        if (readyToGo()) {
            setContentView(R.layout.showmaps);

            text = (TextView) findViewById(R.id.editText1);

            getSupportActionBar().setHomeButtonEnabled(true);

            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);



            map = mapFrag.getMap();
            // map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

            addMarker(map, 23.0333, 72.6167, "Ahmedabad", "");
            addMarker(map, 22.3000, 73.1900, "Baroda", "");

            map.setOnMapClickListener(new OnMapClickListener() {

                @Override
                public void onMapClick(LatLng point) {
                    // TODO Auto-generated method stub
                    map.addMarker(new MarkerOptions().position(point).title(
                            point.toString()));
                    Log.e("TAP MAP---->", "" + point);
                    text.setText("" + point);
                }
            });
        }

        map.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                // TODO Auto-generated method stub
                if (arg0.getTitle().equalsIgnoreCase("Ahmedabad")) {
                    new AlertDialog.Builder(testclassmaps.this)
                            .setTitle("Ahmedabad")
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub

                                        }
                                    }).show();
                }

                if (arg0.getTitle().equalsIgnoreCase("Baroda")) {
                    new AlertDialog.Builder(testclassmaps.this)
                            .setTitle("Baroda")
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub

                                        }
                                    }).show();
                }
                return false;
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void addMarker(GoogleMap map, double lat, double lon,
            String string, String string2) {
        map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                .title(string).snippet(string2));
    }
}

要隐藏信息窗口,请在标记点击事件中使用marker.hideInfoWindow();


如果您不想使用alertDialog,那么可以按照以下方式使用自定义布局来显示信息窗口:

map.setInfoWindowAdapter(new InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker arg0) {

                /*ContextThemeWrapper cw = new ContextThemeWrapper(
                        getApplicationContext(), R.style.Transparent);*/
                // AlertDialog.Builder b = new AlertDialog.Builder(cw);
                LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                View layout = inflater
                        .inflate(R.layout.custom_infowindow, null);
                return layout;
            }

            @Override
            public View getInfoContents(Marker arg0) {

                return null;

            }
        });

-2

我也遇到了同样的问题,经过很长时间的努力,我找到了一个可行的解决方案,也许它对你也有用。

public boolean onMarkerClick(Marker marker) {

    //Please use fix height popup
    float container_height = getResources().getDimension(R.dimen.DIP_300);

    Projection projection = mGoogleMap.getProjection();

    Point markerScreenPosition = projection.toScreenLocation(marker.getPosition());
    Point pointHalfScreenAbove = new Point(markerScreenPosition.x,(int) (markerScreenPosition.y - (container_height / 2)));

    LatLng aboveMarkerLatLng = projection.fromScreenLocation(pointHalfScreenAbove);

    marker.showInfoWindow();
    CameraUpdate center = CameraUpdateFactory.newLatLng(aboveMarkerLatLng);
    mGoogleMap.moveCamera(center);
    mGoogleMap.animateCamera(center);
    marker.showInfoWindow();

    return true;

}

您还可以从下面的链接中查看答案

在这里查看


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。 - Martin Prikryl
Martin Prikryl:感谢您的建议,我已在此处提供答案。 - Hitesh Dhamshaniya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接