Android Map API V2设置自定义InfoWindow位置

时间:2013-06-10 11:50:13

标签: android android-maps android-custom-view android-maps-v2

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

问题是My Custom InfoWindow的大小约为500px * 300px。当我触摸地图上的任何点时,它被设置为屏幕中心,以便从顶部裁剪InfoWindow ..我的要求是根据信息窗口大小自动调整。

请看一下快照。 Issue Snap enter image description here

4 个答案:

答案 0 :(得分:5)

覆盖OnMarkerClickListener的默认行为。

  • 致电marker.showInfoWindow()
  • 使用Projection计算屏幕应居中的位置,并将标记的位置和相机设置为该位置
  • return true

这个答案可以帮助你提出第二点:https://stackoverflow.com/a/16764140/2183804

答案 1 :(得分: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;
            }
        });

答案 2 :(得分:-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;

            }
        });

答案 3 :(得分:-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;

}

您还可以通过以下链接查看答案

Check here