mvasco
mvasco

Reputation: 5101

Is it possible to add markers from an AsyncTask?

There is a Google map on my Android and I am new to the new Google Maps API. There is an initial marker on it. This is the method to show the map and the initial marker:

  @Override
    public void onMapReady(GoogleMap map) {
        //map is ready
        // latitude and longitude
        double latitude = latitud_del_hotel;
        double longitude =longitud_del_hotel;

        // create marker
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(nombre_del_hotel).icon(BitmapDescriptorFactory.fromResource(R.drawable.poi));

        // Changing marker icon
        // marker.icon(BitmapDescriptorFactory
        // .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        map.addMarker(marker);
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(latitud_del_hotel, longitud_del_hotel)).zoom(15).build();
        map.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
    }

Now, I need to put other markers on it. These markers are JSON objects that I am retrieving on another method from the activity.

May be my question is stupid, but I don't know how to add markers on the same map but created on another method.

This is how am I creating a new marker on another method:

 hotel.setNombre(obj.getString("nombre_hotel"));
                                        hotel.setLatitud(obj.getDouble("latitud_hotel"));
                                        hotel.setLongitud(obj.getDouble("longitud_hotel"));
                                        hotel.setDireccion(obj.getString("direccion_hotel"));

                                        String nombre = obj.optString("nombre_hotel");
                                        Log.d("NOMBRE=", nombre);

                                        String latitud = obj.optString("latitud_hotel");
                                        double latitud_del_hotel = Double.parseDouble(latitud);

                                        String longitud = obj.optString("longitud_hotel");
                                        double longitud_del_hotel = Double.parseDouble(longitud);

                                        // create marker
                                        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitud_del_hotel, longitud_del_hotel)).title(nombre).icon(BitmapDescriptorFactory.fromResource(R.drawable.poi));


                                        map.addMarker(marker);

But obviously, the line map.addMarker(marker); shows a warning: Cannot resolve symbol map.

Sorry if it is easy to solve, but I cannot.

Thank you.

Upvotes: 0

Views: 1334

Answers (1)

jyanks
jyanks

Reputation: 2396

In your onMapReady(...) add a field reference to the returned map.

public void onMapReady(GoogleMap map) {
    this.googleMap = map;

Then, you can do:

// create marker
if(this.googleMap != null) {
    MarkerOptions marker = new MarkerOptions(...);
    this.googleMap.addMarker(marker);
}

This might create what's known as a race condition - if your network call to retrieve hotel location information returns a result before the map is ready, you will never get the results added into the map.

You could combat this by doing:

// Create this as a field:
List<MarkerOptions> markerOptions = new ArrayList<>();

MarkerOptions marker = new MarkerOptions(...);
if(this.googleMap != null) {
    this.googleMap.addMarker(marker);
} else {
    this.markerOptions.add(marker);
}

Then, at the bottom of onMapReady(...):

if(this.markerOptions != null && !this.markerOptions.isEmpty()) {
    for(MarkerOption markerOption : this.markerOptions) {
        this.googleMap.addMarker(markerOption);
    }
}

Upvotes: 3

Related Questions