Insert multiple Markers into Maps

Asked

Viewed 53 times

1

I found a code here on the site, I put in my project but it didn’t work, anyone knows how I can solve? The method below is called in onViewCreated()

Note: I am using the Map inside a Fragment, it can be this?

public class MapsFragment extends Fragment implements OnMapReadyCallback {

GoogleMap gMap;
private LatLng latLng;
private Marker marker;
private MarkerOptions markerOptions;
Onibus onibus = new Onibus();

public MapsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_maps, container, false);

    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);
    pedirPermissao();
    carregarLocalizacoes();
}

public void carregarLocalizacoes() {

    ArrayList<LatLng> locations = new ArrayList<LatLng>();

    locations.add(new LatLng(-12.833291, -38.377971));
    locations.add(new LatLng(-12.824711, -38.390898));
    locations.add(new LatLng(-12.795636, -38.404648));

    for (LatLng location : locations) {

        if (location != null) {

            markerOptions.position(location);
            markerOptions.title(onibus.getRoteiro());
            gMap.addMarker(markerOptions);
        }

    }

}

The error that appears is as follows:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.MarkerOptions com.google.android.gms.maps.model.MarkerOptions.position(com.google.android.gms.maps.model.LatLng)' on a null object reference

  • Welcome to Stackoverflow Elailson. Place the link to the question/answer from where you copied the code. Where you initialize the variable markerOptions?

  • https://stackoverflow.com/questions/30569854/adding-multiple-markers-in-google-maps-api-v2-android

  • The variable is initialized just below the class creation line.

  • It would be nice to put that part of the code also in the question, so because that seems to be the problem.

  • I inserted it, but I believe that this is not the problem. The line that accuses the error is: markerOptions.position(Location);

  • Yes, but there is an error on this line because you try to use the object that is stored in the variable markerOptions, but the variable makes no reference to any object. See Lucas' answer.

Show 1 more comment

1 answer

1

You just defined that markerOptions is an object of the type MarkerOptions, you need to instantiate before doing any operation with the same.

Example:

if (location != null) {
      markerOptions = new MarkerOptions();
      markerOptions.position(location);
      markerOptions.title(onibus.getRoteiro());
      gMap.addMarker(markerOptions);
}
  • I made the change, but it hasn’t worked yet :/

  • It is always good to inform the log of the error. Put here for me can help you

  • Now the error went to the last line (Gmap.addMarker(markerOptions);).

  • 1

    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

  • 1

    @Elailsonsilva, it is because you also did not load the variable gMap with no object, it is empty.

  • How do I load it? You are asked for a parameter, I don’t know what it is.

  • In another chunk of code, I can use the Gmap variable without problems, using the instance done right at the beginning of the code.

  • 1

    So put that part of the code in that gMap works, because in this class that you posted the variable is defined but no object is loaded in it.

  • @Elailson Silva, it seems to me you need to study a little more about POO, I recommend you give a read on this link: apostille-java-orientation-objects. To answer your question I recommend you read the following topics of this link: 4.2 Creating a type; 4.3 A class in Java; 4.4 Creating and using an object; 4.5 Methods; 4.6 Methods with return. Post the specific code that works for us to take a look at.

  • Another thing that is always good to do... Whenever you have doubts look for documentation it is much more enlightening and quite rich in content. documentation

  • Another thing that will always help you is that they usually put their sample codes on github and you can see the whole structure of the project that was developed. Github

  • Observing the Github of them, you will see that it is possible to take the Instance of the gMap, or better said the Googlemap, at: public void onMapReady(GoogleMap googleMap)

  • Look there: Example

Show 8 more comments

Browser other questions tagged

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