Make Googlemap’s Mark Draggabe As Well As Uncovering Moving Marker

Posted on

To make GoogleMap’s marking draggabe too honor moving marker:
– Modify MapsActivity to implement GoogleMap.OnMarkerDragListener
– Override onMarkerDragStart(Marker marker), onMarkerDrag(Marker marker) too onMarkerDragEnd(Marker marker), to induce got the MarkerDrag events.
– Assign OnMarkerDragListener yesteryear calling mMap.setOnMarkerDragListener(this).
– Set markers draggable yesteryear calling markerOptions.draggable(true).


Modify MapsActivity.java from in conclusion post service “Detect deportment upon GoogleMap, onMapClick() too onMapLongClick()“.

MapsActivity.java

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.Marker; import com.google.android.gms.maps.model.MarkerOptions;  world course of written report MapsActivity extends AppCompatActivity implements OnMapReadyCallback,         GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener,         GoogleMap.OnMarkerDragListener {      someone GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment too larn notified when the map is laid upwards to endure used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map ane time available.      * This callback is triggered when the map is laid upwards to endure used.      * This is where nosotros tin add together markers or lines, add together listeners or motion the camera. In this case,      * nosotros only add together a marking close Sydney, Australia.      * If Google Play services is non installed on the device, the user volition endure prompted to install      * it within the SupportMapFragment. This method volition entirely endure triggered ane time the user has      * installed Google Play services too returned to the app.      */     @Override     world void onMapReady(GoogleMap googleMap) {         mMap = googleMap;         mMap.setOnMapClickListener(this);         mMap.setOnMapLongClickListener(this);         mMap.setOnMarkerDragListener(this);     }      @Override     world boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         provide super.onCreateOptionsMenu(menu);     }      @Override     world boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             illustration R.id.menu_addmarkers:                 addMarker();                 provide true;             illustration R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     provide true;                 }             illustration R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     provide true;                 }             illustration R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     provide true;                 }             illustration R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     provide true;                 }             illustration R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     provide true;                 }             illustration 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;             illustration 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();                  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);              terminal EditText titleField = novel EditText(MapsActivity.this);             titleField.setHint("Title");              terminal 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);              terminal 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);     } }  

reference: https://developers.google.com/maps/documentation/android-api/marker

Next: – Custom InfoWindow for Google Maps Android API v2


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