Detect Demeanour Upon Googlemap, Onmapclick() In Addition To Onmaplongclick()

Posted on

To detect impact GoogleMap, alter MapsActivity.java from in conclusion instance “Google Maps Android API v2 – initialize map inwards xml“.

Edit MapsActivity to implements GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener.

Implement your listeners, onMapClick() and onMapLongClick().

Assign the listeners to your GoogleMap past times calling:
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);

package com.blogspot.android_er.androidstudiomapapp;  import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; 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.widget.EditText; import android.widget.LinearLayout; 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.MarkerOptions;  world degree MapsActivity extends AppCompatActivity implements OnMapReadyCallback,         GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener {      somebody GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment in addition to instruct notified when the map is cook to hold upwards used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map i time available.      * This callback is triggered when the map is cook to hold upwards used.      * This is where nosotros tin laissez passer notice add together markers or lines, add together listeners or movement the camera. In this case,      * nosotros but add together a mark close Sydney, Australia.      * If Google Play services is non installed on the device, the user volition hold upwards prompted to install      * it within the SupportMapFragment. This method volition solely hold upwards triggered i time the user has      * installed Google Play services in addition to returned to the app.      */     @Override     world void onMapReady(GoogleMap googleMap) {         mMap = googleMap;         mMap.setOnMapClickListener(this);         mMap.setOnMapLongClickListener(this);     }      @Override     world boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         supply super.onCreateOptionsMenu(menu);     }      @Override     world boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             instance R.id.menu_addmarkers:                 addMarker();                 supply true;             instance R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     supply true;                 }             instance R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     supply true;                 }             instance R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     supply true;                 }             instance R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     supply true;                 }             instance R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     supply 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();                 supply 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();                  supply true;         }         supply super.onOptionsItemSelected(item);     }      somebody 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);              in conclusion EditText titleField = novel EditText(MapsActivity.this);             titleField.setHint("Title");              in conclusion 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);              in conclusion 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 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);                          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 mark on LongClick seat         MarkerOptions markerOptions =                 novel MarkerOptions().position(latLng).title(latLng.toString());         mMap.addMarker(markerOptions);     } }  

Next: Make GoogleMap’s mark draggabe in addition to break moving marker


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