Google Maps Android API v2 detect long press on map and add marker not working

拟墨画扇 提交于 2019-12-05 00:21:06
Shadow

I used this. It worked.

GoogleMap gm;

gm.setOnMapLongClickListener(this); 

@Override    
public void onMapLongClick(LatLng point) {
    gm.addMarker(new MarkerOptions()
        .position(point)
        .title("You are here")           
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));  
}

You have no icon setted. Try:

    @Override
    public void onMapLongClick(LatLng point) {

        myMap.addMarker(new MarkerOptions()
            .position(point)
            .title(point.toString())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 

         Toast.makeText(getApplicationContext(),
            "New marker added@" + point.toString(), Toast.LENGTH_LONG)
            .show();

} 
googleMap = supportMapFragment.getMap();

    // Setting a click event handler for the map
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latLng) {

            // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting the position for the marker
            markerOptions.position(latLng);

            // Setting the title for the marker.
            // This will be displayed on taping the marker
            markerOptions.title(latLng.latitude + " : " + latLng.longitude);

            // Clears the previously touched position
            googleMap.clear();

            // Animating to the touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            // Placing a marker on the touched position
            googleMap.addMarker(markerOptions);
        }
    });

OR

mMap.setOnMapLongClickListener(new
           GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick (LatLng latLng){
           Geocoder geocoder =
              new Geocoder(MapMarkerActivity.this);
           List<Address> list;
           try {
              list = geocoder.getFromLocation(latLng.latitude,
                 latLng.longitude, 1);
           } catch (IOException e) {
              return;
           }
           Address address = list.get(0);
           if (marker != null) {
              marker.remove();
           }

           MarkerOptions options = new MarkerOptions()
              .title(address.getLocality())
              .position(new LatLng(latLng.latitude,
                 latLng.longitude));

           marker = mMap.addMarker(options);
        }
     });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!