简体   繁体   中英

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.

@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?

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM