Android Map API V2 set Custom InfoWindow Position

十年热恋 提交于 2020-01-01 19:04:25

问题


I'm using InfoWindowAdapter for all the markers on my Map (Api v2). All the Markers are well visible..

The issue is that My Custom InfoWindow is having size about 500px * 300px. When I'm touching any Points on the Map its being set to Center of screen so the InfoWindow is being cropped from Top.. My requirement is to auto adjust as per Info Window Size.

Please have a look at snaps.


回答1:


Override the default behaviour of OnMarkerClickListener.

  • call marker.showInfoWindow()
  • calculate position on which screen should be centered using Projection and position of the marker and animate camera to that position
  • return true

This answer should help you with second point: https://stackoverflow.com/a/16764140/2183804




回答2:


I had the same issue, I tried the following perfectly working solution

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



回答3:


check code below: i have used simple alertDialog you can use custom or whatever you want.

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

to hide info window use marker.hideInfoWindow(); inside marker click


if you don't want to use alertDialog then you can use custom layout for info window below way:

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;

            }
        });



回答4:


I also faced same problem and after long time I got working solution, may be it also work for

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;

}

you can also see answer from below link

Check here



来源:https://stackoverflow.com/questions/17023222/android-map-api-v2-set-custom-infowindow-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!