Jay
Jay

Reputation: 145

Android map : add markers

I have two classes, one inside the other. The first class has some implements to manage the map and inside it I've done the ovverride of all the methods:

public class Principal extends AppCompatActivity implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    GoogleMap.OnMarkerDragListener,
    GoogleMap.OnMapLongClickListener,
    View.OnClickListener {

         //Our Map
          public GoogleMap mMap; 

          ...

          class PostAsync extends AsyncTask<String, String, JSONObject> {

                  ...

          }
}

Inside this class i implemented a second class that I use for get data from my database.

class PostAsync extends AsyncTask<String, String, JSONObject> {}

The database returns some coordinates inside this class and I need to add some markers in the map of the Principal class. Problem: I can't add markers from the PostAsync class. So how can I do ?

Thanks.

Upvotes: 1

Views: 358

Answers (2)

C.Ozyurt
C.Ozyurt

Reputation: 21

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

}

class PostAsync extends AsyncTask<String, String, JSONObject> {

    @Override
    protected void onPostExecute(JSONObject jsonObject) {

        ... // parse json

        MarkerOptions markerOptions = new MarkerOptions();
        LatLng latLng = new LatLng(20, 30);
        mMap.addMarker(markerOptions
                .position(latLng)
                .title("Title")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                ...
    }
}

Would you try like this?

Upvotes: 1

antonio
antonio

Reputation: 18242

Markers can be added on the main (UI) thread, and the onPostExecute method of the AsyncTask executes of the main (UI) thread.

So, according to your definition of your PostAsync class (extends AsyncTask<String, String, JSONObject>):

class PostAsync extends AsyncTask<String, String, JSONObject> {

    // ...

    protected void onPostExecute(JSONObject result) {
        // Decode your JSONObject and add Markers to the map here
    }
}

Upvotes: 1

Related Questions