3

我正在为我的地图(Api v2)上的所有标记使用 InfoWindowAdapter。所有标记都清晰可见..

问题是我的自定义信息窗口的大小约为 500 像素 * 300 像素。当我触摸地图上的任何点时,它被设置为屏幕中心,因此信息窗口被从顶部裁剪。我的要求是根据信息窗口大小自动调整。

请看一下快照。 问题快照在此处输入图像描述

4

4 回答 4

5

覆盖 的默认行为OnMarkerClickListener

  • 称呼marker.showInfoWindow()
  • 计算应该在哪个屏幕上居中Projection的位置,使用标记的位置和动画相机到该位置
  • 返回true

这个答案应该可以帮助您解决第二点:https ://stackoverflow.com/a/16764140/2183804

于 2013-06-10T22:22:41.570 回答
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:23:33.110 回答
-1

检查下面的代码:我使用了简单的 alertDialog 你可以使用自定义或任何你想要的。

在此处输入图像描述

在此处输入图像描述

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;

            }
        });
于 2013-06-10T12:56:48.840 回答
-2

我也遇到了同样的问题,经过很长时间我得到了有效的解决方案,可能它也适用于

公共布尔 onMarkerClick(标记标记){

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

}

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

在这里检查

于 2014-03-12T13:44:17.443 回答