Add Marker on Android Google Map via touch or tap

2019-01-13 21:59发布

问题:

I am a beginner in Android Developer. I want to develop a Map Application using Google Map. Now, I want to add marker on the map via Touch or Tap on the Map. I do not know how to apply touch event to drop the marker on the map. Please tell me and, if it is possible, please give me some links or examples. Thank you and sorry for my English.

回答1:

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)); 
}


回答2:

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.



回答3:

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



回答4:

Try these Tutorial 1 and Tutorial 2for understanding of Google maps

and refer this link to Stack Overflow Question so as to do it on onTouch