简体   繁体   English

在Google地图中的标记上显示当前位置/地址/区域名称

[英]Display the current location / address / area name on a Marker in google map

I created a basic application which displays a google map as well as a marker. 我创建了一个基本应用程序,它显示谷歌地图和标记。 The marker appears on the map clearing the previous location wherever I click. 无论我点击哪里,标记都会显示在地图上,清除之前的位置。 Now, I am trying to display the location name / address / area name on the marker wherever I click on the google map. 现在,我试图在标记处显示位置名称/地址/区域名称,只要我点击谷歌地图。 But my application is getting closed unfortunately.I used a toast temporary for confirming the display. 但遗憾的是我的应用程序正在关闭。我暂时使用Toast来确认显示。 The listener I used is this : 我使用的听众是这样的:

Temporary.java file Temporary.java文件

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {
                 List<Address> address = null;
                 Geocoder geocode;
                // TODO Auto-generated method stub
                geocode = new Geocoder(Temporary.this,Locale.getDefault());
                try {
                    address=geocode.getFromLocation(currentGPS.latitude, currentGPS.longitude, 1);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String address1=address.get(0).getCountryName();
                Toast.makeText(Temporary.this,address1, Toast.LENGTH_LONG).show();
                return false;
            }
        });

I tried one more Listener which is as follows : 我又尝试了一个Listener,如下所示:

        googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            List<Address> address = null;
             Geocoder geocode;
            // TODO Auto-generated method stub
            geocode = new Geocoder(Temporary.this,Locale.getDefault());
            try {
                address=geocode.getFromLocation(point.latitude, point.longitude, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             MarkerOptions markerOptions = new MarkerOptions();
             markerOptions.position(point);
             markerOptions.title("Area Name :"+address.get(0).getLocality());
             googleMap.clear();
             googleMap.addMarker(markerOptions);

        }
    });

Still its not working. 仍然没有用。 Where I am mistaken? 哪里弄错了?

you can set up a InfoWindow over your map and customize it on the current selection. 您可以在地图上设置InfoWindow,并在当前选择中对其进行自定义。

        map.setInfoWindowAdapter(new InfoWindowAdapter() {

                    @Override
                    public View getInfoWindow(Marker arg0) {
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker arg0) {
                        currentInfo = getLayoutInflater().inflate(R.layout.info_layout, null);
                        //Do what you want with the view
                        return currentInfo;
                    }
        }

Remember the marker has his own location, title, color, etc. 记住标记有自己的位置,标题,颜色等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM