Add Marker on Android Google Map via touch or tap
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));
}
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!!!