34

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
shiteru
  • 635
  • 3
  • 9
  • 15
  • 3
    Does this answer your question? [Add marker on touched location using google map in Android](https://stackoverflow.com/questions/2171429/add-marker-on-touched-location-using-google-map-in-android) – Cœur Nov 15 '19 at 14:19

3 Answers3

82

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.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Sharanu
  • 881
  • 6
  • 7
20

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

Max Base
  • 639
  • 1
  • 7
  • 15
Akanksha Rathore
  • 3,603
  • 3
  • 31
  • 47
5

This code is Successful run I am working on that code this code is for Dynamic Draw

I think this code help you more for Static or dynamic both places you can use this code

double latval = Double.parseDouble(jsonobject.getString("lat"));
double longval = Double.parseDouble(jsonobject.getString("lon"));

mMap.addMarker(new MarkerOptions()
               .position(new LatLng( latval,    longval))
               .title(jsonobject.getString("country"))
               .snippet("4 E. 28TH Street From $15 /per night")
               .rotation((float) -15.0)
               .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
              );

if (i == 0) {
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                                new LatLng(latval, longval), 7));
    mMap.addCircle(new CircleOptions()
                   .center(new LatLng(latval,longval))
                   .radius(5000)
                   .strokeColor(Color.RED)
                   .fillColor(Color.RED)); 
}
Charuක
  • 12,953
  • 5
  • 50
  • 88
user3066085
  • 128
  • 1
  • 8
  • 3
    @shiteru This should not be the accepted answer. It does not answer the original question, but this answer does: https://stackoverflow.com/a/17147121/2102748. It may have helped you solve the problem, but it does not contain the answer to the question and is misleading for new visitors. – milosmns Oct 30 '18 at 15:29
  • how is this the accepted answer? it does not answer the question at all. – AwaisMajeed Jan 03 '21 at 19:05