Search code examples
javaandroidgoogle-mapsgoogle-places-apimarker

Android. Google map API implement on marker click


I use google places API to search nearby hospitals. I need to display a custom dialog with the information that I received in the request of the nearest hospitals by clicking on the marker. But in the onMarkerClick(Marker marker) method there is only such information about the marker as title, position etc. But I need to show also the address, photo and other parameters from nearby hospitals request, how can this be implemented.


Solution

  • You can add an info window to display place information using the marker's snippet property upon clicking on a marker, like this:

    static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
    Marker melbourne = mMap.addMarker(new MarkerOptions()
                              .position(MELBOURNE)
                              .title("Melbourne")
                              .snippet("Population: 4,137,400")); 
    

    Replace the hard-coded string with the place's information retrieved from nearby search that you want to display. You can also do:

    @Override
    public boolean onMarkerClick(Marker marker) {
       Toast.makeText(getApplicationContext(), marker.getSnippet(), Toast.LENGTH_LONG).show();
       return false;
    }
    

    There are multiple ways to achieve this but this would be the easiest. You're also able to customize the info windows' content using getInfoContents().