Why can’t I add a marker by clicking on the map?

Asked

Viewed 199 times

0

I’m using this code:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import static com.marcadorfixo.R.id.map;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    GoogleMap googleMap;

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

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
        mapFragment.getMapAsync(MainActivity.this);

        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng position) {
                googleMap.addMarker(
                        new MarkerOptions().position(position)
                );
            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng uaua = new LatLng(-9.840318, -39.480388);
        googleMap.addMarker(new MarkerOptions()
                .position(uaua)
                .title("Uauá"));
    }
}

Android Studio even compiles, but when trying to run the application it simply stops working.

  • 2

    What error presented by android studio?

  • Dude, he doesn’t accuse error, that’s what I don’t understand. For example: He compiles, no error, then when I run the app it stops working. Like, if he did, and when he clicked on the map, he would stop and I could look for the problem myself, but not even that... I’ve looked for some tutorials, but it’s scarce.

1 answer

1


The problem is that you are adding the click on an object null that was not instantiated you can only add the click after receiving the object in the method onMapReady. Try the solution below:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import static com.marcadorfixo.R.id.map;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    //GoogleMap googleMap;

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

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
        mapFragment.getMapAsync(MainActivity.this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng uaua = new LatLng(-9.840318, -39.480388);

        googleMap.addMarker(new MarkerOptions()
                .position(uaua)
                .title("Uauá"));

        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng position) {
                googleMap.addMarker(
                        new MarkerOptions().position(position)
                );
            }
        });
    }
}
  • Man, thanks a lot. You solved my problem. Thanks a lot

Browser other questions tagged

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