3

In my app I detect user's current location automatically and center the map around the marker.

I want to enable the user to click elsewhere on the map, and the marker to appear at the position they clicked, and the lat/lon to be updated to that new position.

How do I do that

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

2 Answers2

10

try this

 Marker marker;
 GoogleMap mMap;

 mMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latlng) {
            // TODO Auto-generated method stub

            if (marker != null) {
                marker.remove();
            }
            marker = mMap.addMarker(new MarkerOptions()
                    .position(latlng)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            System.out.println(latlng);

       }
   });
Rakesh Rangani
  • 1,039
  • 10
  • 13
  • This works, but the blue point that indicates my position remains at my position. and the new marker is not a blue dot, but a red water drop thingy upside down? – Kaloyan Roussev Aug 20 '14 at 13:41
2

In order to add a marker with the current location you have to implement LocationListener

With the following code you can add a marker with your location and move the camera:

            public void onLocationChanged(Location location) {

            map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
                    .title("My Location"));

            /* ..and Animate camera to center on that location !
             * (the reason for we created this custom Location Source !) */
            map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
        }

In order to add a maker when the user taps inside the map you can use OnMapLongClickListener

    @Override
    public void onMapLongClick(LatLng point) {

        mMap.addMarker(new MarkerOptions()
            .position(point)
            .snippet(""));
    }
Ariel Carbonaro
  • 1,529
  • 1
  • 13
  • 25
  • Yes, [OnMapClickListener](http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.OnMapClickListener.html) – Ariel Carbonaro Aug 20 '14 at 13:43