Search code examples
javaandroid-studiogoogle-mapsdrag-and-dropgoogle-maps-markers

mMap.setonMarkerDragListener is making my application crash


I'm doing an application where i want to get the longitude and latitude from google maps. i added the map to my activity and it works but when i added the marker drag listener the application crashes. here's my code:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    Button go;
     GoogleMap mMap;
     ActivityMapsBinding binding;
    Double latitude,longitude;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        binding = ActivityMapsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
    @Override
    public void onMarkerDragStart(@NonNull @NotNull Marker marker) {

    }

    @Override
    public void onMarkerDrag(@NonNull @NotNull Marker marker) {

    }

    @Override
    public void onMarkerDragEnd(@NonNull @NotNull Marker marker) {
       latitude =  marker.getPosition().latitude;
        longitude  =  marker.getPosition().longitude;
    }
});

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

        go = findViewById(R.id.button1);
        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_to_act2();
            }
        });
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @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").draggable(true));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

i tried to comment the on marker drag and the application worked


Solution

  • Move this statement:

    mMap.setOnMarkerDragListener(...
    

    to inside your onMapReady after the mMap is initialized, such as:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
    
        mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(@NonNull @NotNull Marker marker) {
            }
    
            @Override
            public void onMarkerDrag(@NonNull @NotNull Marker marker) {
            }
    
            @Override
            public void onMarkerDragEnd(@NonNull @NotNull Marker marker) {
                latitude =  marker.getPosition().latitude;
                longitude  =  marker.getPosition().longitude;
            }
        });
    
        // 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").draggable(true));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
    

    You cannot use mMap until it is initialized and that is done asynchronously in the onMapReady.