I want to develop a Map Application using Google Map on Android. Now, I want to add marker on the map via Touch or Tap on the Map. How do I apply touch event to drop the marker on the map?
The transition() and moveMarker() are used to move marker smoothly on click on the Google map.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
Try using new Google Map API v2.
It's easy to use and you can add a marker on tap like this:
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { allPoints.add(point); map.clear(); map.addMarker(new MarkerOptions().position(point)); } });
or in Kotlin:
map.setOnMapClickListener { allPoints.add(it) map.clear() map.addMarker(MarkerOptions().position(it)) }
Note that you might want to remember all your added points in a list (allPoints
), so you can re-draw or remove them later. An even better approach to remember the points would be to remember a Marker
object for each of them - you can get the Marker
object as a result from the addMarker
function, it has a remove()
function that easily removes the marker from the map.
The technique which i used is:
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { MarkerOptions marker = new MarkerOptions().position(new LatLng(point.latitude, point.longitude)).title("New Marker"); googleMap.addMarker(marker); System.out.println(point.latitude+"---"+ point.longitude); } });
Hope it helps!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With