Add Marker with click

Asked

Viewed 61 times

1

How do I add a marker to the map with a click?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_maps);


       // Obtain the SupportMapFragment and get notified when the map is ready to be used.
       SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
       mapFragment.getMapAsync(this);
}



   @Override
   public void onMapReady(GoogleMap googleMap) {
       mMap = googleMap;

      // Add a marker in Sydney and move the camera
       LatLng sydney = new LatLng(-34, 151);
       mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
       mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }


}

1 answer

0

Within the method onMapRead just use the method setOnMapClickListener() including the settings of Marker, passing latitude and longitude. See:

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
        // define latitude longitude e titulo do marker
        MarkerOptions marker = new MarkerOptions().position(
            new LatLng(point.latitude, point.longitude)).title("New Marker");
        // inclui um marker
        googleMap.addMarker(marker);

    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.