Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Marker on Android Google Map via touch or tap

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?

like image 777
shiteru Avatar asked Jun 17 '13 08:06

shiteru


People also ask

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

How do I add a marker on Google Maps Android?

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.


2 Answers

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.

like image 66
Sharanu Avatar answered Oct 02 '22 14:10

Sharanu


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!!!

like image 42
Akanksha Rathore Avatar answered Oct 02 '22 16:10

Akanksha Rathore