Custom Infowindow For Google Maps Android Api V2

Posted on

To create Custom InfoWindow for Google Maps Android API v2:

– Make your Activity implements GoogleMap.InfoWindowAdapter.

– Override getInfoWindow(Marker marker) and getInfoContents(Marker marker).

The API volition start telephone telephone getInfoWindow(Marker) as well as if nothing is returned, it volition as well as then telephone telephone getInfoContents(Marker). If this equally good returns null, as well as then the default information window volition live used.

The start of these (getInfoWindow()) allows y’all to furnish a persuasion that volition live used for the entire information window. The minute of these (getInfoContents()) allows y’all to merely customize the contents of the window but notwithstanding proceed the default information window frame as well as background.

– setInfoWindowAdapter(this).


Modify MapsActivity.java from lastly illustration “Make GoogleMap’s marking draggabe as well as uncovering moving marker“.

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;  world degree 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 as well as teach notified when the map is cook to live used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map 1 time available.      * This callback is triggered when the map is cook to live used.      * This is where nosotros tin add together markers or lines, add together listeners or motility the camera. In this case,      * nosotros merely add together a marking nigh Sydney, Australia.      * If Google Play services is non installed on the device, the user volition live prompted to install      * it within the SupportMapFragment. This method volition entirely live triggered 1 time the user has      * installed Google Play services as well as returned to the app.      */     @Override     world void onMapReady(GoogleMap googleMap) {         mMap = googleMap;         mMap.setOnMapClickListener(this);         mMap.setOnMapLongClickListener(this);         mMap.setOnMarkerDragListener(this);         mMap.setInfoWindowAdapter(this);     }      @Override     world boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         render super.onCreateOptionsMenu(menu);     }      @Override     world boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             instance R.id.menu_addmarkers:                 addMarker();                 render true;             instance R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     render true;                 }             instance R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     render true;                 }             instance R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     render true;                 }             instance R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     render true;                 }             instance R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     render 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();                 render 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                     world 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                     world void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                  AlertDialog aboutDialog = aboutDialogBuilder.create();                 aboutDialog.show();                  render true;         }         render 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                 world 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 comprise a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      try{                         lon = Double.parseDouble(strLon);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Longitude does non comprise 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     world void onMapClick(LatLng latLng) {         Toast.makeText(MapsActivity.this,                 "onMapClick:\n" + latLng.latitude + " : " + latLng.longitude,                 Toast.LENGTH_LONG).show();     }      @Override     world 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     world void onMarkerDragStart(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(0.5f);     }      @Override     world void onMarkerDrag(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(0.5f);     }      @Override     world void onMarkerDragEnd(Marker marker) {         marker.setTitle(marker.getPosition().toString());         marker.showInfoWindow();         marker.setAlpha(1.0f);     }      @Override     world View getInfoWindow(Marker marker) {         render null;         //return prepareInfoView(marker);     }      @Override     world View getInfoContents(Marker marker) {         //return null;         render prepareInfoView(marker);      }      private 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);          render infoView;     } }  

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

Next:
– Detect user click on InfoWindow, yesteryear implementing GoogleMap.OnInfoWindowClickListener()


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