Detect User Click On Infowindow, Past Times Implementing Googlemap.Oninfowindowclicklistener()

Posted on


To give away user tap on Google Maps InfoWindow, implement your GoogleMap.OnInfoWindowClickListener() together with telephone telephone GoogleMap.setOnInfoWindowClickListener(OnInfoWindowClickListener).

Modify from finally representative “Custom InfoWindow for Google Maps Android API v2“:

MapsActivity.java

package com.blogspot.android_er.androidstudiomapapp;  import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.InputType; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;  import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions;  populace shape MapsActivity extends AppCompatActivity implements OnMapReadyCallback,         GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener,         GoogleMap.OnMarkerDragListener, GoogleMap.InfoWindowAdapter {      someone GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment together with larn notified when the map is cook to survive used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map in ane trial available.      * This callback is triggered when the map is cook to survive used.      * This is where nosotros tin add together markers or lines, add together listeners or motility the camera. In this case,      * nosotros only add together a marking nigh Sydney, Australia.      * If Google Play services is non installed on the device, the user volition survive prompted to install      * it within the SupportMapFragment. This method volition solely survive triggered in ane trial the user has      * installed Google Play services together with returned to the app.      */     @Override     populace void onMapReady(GoogleMap googleMap) {         mMap = googleMap;         mMap.setOnMapClickListener(this);         mMap.setOnMapLongClickListener(this);         mMap.setOnMarkerDragListener(this);         mMap.setInfoWindowAdapter(this);         mMap.setOnInfoWindowClickListener(MyOnInfoWindowClickListener);     }      @Override     populace boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         provide super.onCreateOptionsMenu(menu);     }      @Override     populace boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             instance R.id.menu_addmarkers:                 addMarker();                 provide true;             instance R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     provide true;                 }             instance R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     provide true;                 }             instance R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     provide true;                 }             instance R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     provide true;                 }             instance R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     provide true;                 }             instance R.id.menu_legalnotices:                 String LicenseInfo = GoogleApiAvailability                         .getInstance()                         .getOpenSourceSoftwareLicenseInfo(MapsActivity.this);                 AlertDialog.Builder LicenseDialog =                         novel AlertDialog.Builder(MapsActivity.this);                 LicenseDialog.setTitle("Legal Notices");                 LicenseDialog.setMessage(LicenseInfo);                 LicenseDialog.show();                 provide true;             instance R.id.menu_about:                 AlertDialog.Builder aboutDialogBuilder =                         novel AlertDialog.Builder(MapsActivity.this);                 aboutDialogBuilder.setTitle("About Me")                         .setMessage("https://rootandroid.org/");                  aboutDialogBuilder.setPositiveButton("visit",                         novel DialogInterface.OnClickListener() {                     @Override                     populace void onClick(DialogInterface dialog, int which) {                         String url = "https://rootandroid.org/";                         Intent i = novel Intent(Intent.ACTION_VIEW);                         i.setData(Uri.parse(url));                         startActivity(i);                     }                 });                  aboutDialogBuilder.setNegativeButton("Dismiss",                         novel DialogInterface.OnClickListener() {                     @Override                     populace void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                  AlertDialog aboutDialog = aboutDialogBuilder.create();                 aboutDialog.show();                  provide true;         }         provide super.onOptionsItemSelected(item);     }      someone void addMarker(){         if(mMap != null){              //create custom LinearLayout programmatically             LinearLayout layout = novel LinearLayout(MapsActivity.this);             layout.setLayoutParams(new LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT,                     LinearLayout.LayoutParams.WRAP_CONTENT));             layout.setOrientation(LinearLayout.VERTICAL);              concluding EditText titleField = novel EditText(MapsActivity.this);             titleField.setHint("Title");              concluding EditText latField = novel EditText(MapsActivity.this);             latField.setHint("Latitude");             latField.setInputType(InputType.TYPE_CLASS_NUMBER                     | InputType.TYPE_NUMBER_FLAG_DECIMAL                     | InputType.TYPE_NUMBER_FLAG_SIGNED);              concluding EditText lonField = novel EditText(MapsActivity.this);             lonField.setHint("Longitude");             lonField.setInputType(InputType.TYPE_CLASS_NUMBER                     | InputType.TYPE_NUMBER_FLAG_DECIMAL                     | InputType.TYPE_NUMBER_FLAG_SIGNED);              layout.addView(titleField);             layout.addView(latField);             layout.addView(lonField);              AlertDialog.Builder builder = novel AlertDialog.Builder(this);             builder.setTitle("Add Marker");             builder.setView(layout);             AlertDialog alertDialog = builder.create();              builder.setPositiveButton("OK", novel DialogInterface.OnClickListener() {                 @Override                 populace void onClick(DialogInterface dialog, int which) {                     boolean parsable = true;                     Double lat = null, lon = null;                      String strLat = latField.getText().toString();                     String strLon = lonField.getText().toString();                     String strTitle = titleField.getText().toString();                      try{                         lat = Double.parseDouble(strLat);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Latitude does non incorporate a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      try{                         lon = Double.parseDouble(strLon);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Longitude does non incorporate a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      if(parsable){                          LatLng targetLatLng = novel LatLng(lat, lon);                         MarkerOptions markerOptions =                                 novel MarkerOptions().position(targetLatLng).title(strTitle);                          markerOptions.draggable(true);                          mMap.addMarker(markerOptions);                         mMap.moveCamera(CameraUpdateFactory.newLatLng(targetLatLng));                      }                 }             });             builder.setNegativeButton("Cancel", null);              builder.show();         }else{             Toast.makeText(MapsActivity.this, "Map non ready", Toast.LENGTH_LONG).show();         }     }      @Override     populace void onMapClick(LatLng latLng) {         Toast.makeText(MapsActivity.this,                 "onMapClick:\n" + latLng.latitude + " : " + latLng.longitude,                 Toast.LENGTH_LONG).show();     }      @Override     populace void onMapLongClick(LatLng latLng) {         Toast.makeText(MapsActivity.this,                 "onMapLongClick:\n" + latLng.latitude + " : " + latLng.longitude,                 Toast.LENGTH_LONG).show();          //Add marking on LongClick seat         MarkerOptions markerOptions =                 novel MarkerOptions().position(latLng).title(latLng.toString());         markerOptions.draggable(true);          mMap.addMarker(markerOptions);     }       @Override     populace void onMarkerDragStart(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(0.5f);     }      @Override     populace void onMarkerDrag(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(0.5f);     }      @Override     populace void onMarkerDragEnd(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(1.0f);     }      @Override     populace View getInfoWindow(Marker marker) {         provide null;         //return prepareInfoView(marker);     }      @Override     populace View getInfoContents(Marker marker) {         //return null;         provide prepareInfoView(marker);      }      someone View prepareInfoView(Marker marker){         //prepare InfoView programmatically         LinearLayout infoView = novel LinearLayout(MapsActivity.this);         LinearLayout.LayoutParams infoViewParams = novel LinearLayout.LayoutParams(                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);         infoView.setOrientation(LinearLayout.HORIZONTAL);         infoView.setLayoutParams(infoViewParams);          ImageView infoImageView = novel ImageView(MapsActivity.this);         //Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);         Drawable drawable = getResources().getDrawable(android.R.drawable.ic_dialog_map);         infoImageView.setImageDrawable(drawable);         infoView.addView(infoImageView);          LinearLayout subInfoView = novel LinearLayout(MapsActivity.this);         LinearLayout.LayoutParams subInfoViewParams = novel LinearLayout.LayoutParams(                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);         subInfoView.setOrientation(LinearLayout.VERTICAL);         subInfoView.setLayoutParams(subInfoViewParams);          TextView subInfoLat = novel TextView(MapsActivity.this);         subInfoLat.setText("Lat: " + marker.getPosition().latitude);         TextView subInfoLnt = novel TextView(MapsActivity.this);         subInfoLnt.setText("Lnt: " + marker.getPosition().longitude);         subInfoView.addView(subInfoLat);         subInfoView.addView(subInfoLnt);         infoView.addView(subInfoView);          provide infoView;     }      GoogleMap.OnInfoWindowClickListener MyOnInfoWindowClickListener             = novel GoogleMap.OnInfoWindowClickListener(){         @Override         populace void onInfoWindowClick(Marker marker) {             Toast.makeText(MapsActivity.this,                     "onInfoWindowClick():\n" +                     marker.getPosition().latitude + "\n" +                     marker.getPosition().longitude,                     Toast.LENGTH_LONG).show();         }     };  }  

Reference: https://developers.google.com/maps/documentation/android-api/infowindows#info_window_events

Next:
– Display StreetViewPanoramaView inwards DialogFragment, when user click on InfoWindow


Step-by-step of Android Google Maps Activity using Google Maps Android API v2, on Android Studio