Google Maps Android API v2 detecta un clic largo en el mapa y agrega marcador que no funciona

Quiero agregar marcador en el mapa con un clic largo. Pero no está funcionando. Normal de clic está trabajando. Toast en onMapClick() se muestra con un toque normal. Pero el clic largo no funciona. Toast en onMapLongClick() no se muestra con un clic largo. También el marcador no se muestra en el mapa.

Estoy utilizando SupportMapFragment porque quiero usar mi aplicación en dispositivos android 2.x. He probado mi aplicación en el nexo uno que tiene Android versión 2.3.7.

Este es mi código.

 public class MainActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener { final int RQS_GooglePlayServices = 1; private GoogleMap myMap; Location myLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager myFragmentManager = getSupportFragmentManager(); SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager .findFragmentById(R.id.map); myMap = mySupportMapFragment.getMap(); myMap.setMyLocationEnabled(true); myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); // myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); // myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); myMap.setOnMapClickListener(this); myMap.setOnMapLongClickListener(this); } @Override protected void onResume() { super.onResume(); int resultCode = GooglePlayServicesUtil .isGooglePlayServicesAvailable(getApplicationContext()); if (resultCode == ConnectionResult.SUCCESS) { Toast.makeText(getApplicationContext(), "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG) .show(); } else { GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onMapClick(LatLng point) { myMap.animateCamera(CameraUpdateFactory.newLatLng(point)); Toast.makeText(getApplicationContext(), point.toString(), Toast.LENGTH_LONG).show(); } @Override public void onMapLongClick(LatLng point) { myMap.addMarker(new MarkerOptions().position(point).title( point.toString())); Toast.makeText(getApplicationContext(), "New marker added@" + point.toString(), Toast.LENGTH_LONG) .show(); } } 

Como puedo resolver esto ?

He usado esto. Funcionó.

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

No tienes ningún icono establecido. Tratar:

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

O

 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); } }); 
  • No se puede cambiar el identificador de contenedor del fragmento SupportMapFragment
  • SupportMapFragment y onMapReady
  • Android - SupportMapFragment con GoogleMaps API 2.0 dando IllegalArgumentException
  • Referencia MapView en SupportMapFragment
  • Estado de error: active SupportMapFragment {} ha borrado el índice: -1
  • Android MapFragment Runtime Error - No se pudo encontrar la clase 'maps.jk'
  • Android GoogleMap o SupportMapFragment - excepción de puntero nulo
  • ClassNotFoundException leyendo un objeto Serializable en una clase que extiende MapFragment en onSaveInstanceState
  • El método getSupportFragmentManager () no está definido para el tipo MapFragment
  • Google map se estrella en pocos dispositivos
  • Android google maps nullpointerexception
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.