Skip to content Skip to sidebar Skip to footer

Android Studio Execute Asyncclass Before Onmapready

I have this code where i have to call data from database yung asynctask the coordinates for my googlemap. but the onMapReady is executing before my Asynctask so the location is alw

Solution 1:

There is no way to guarantee that your async task could complete before the Google map's onMapReady() were called. Instead, I propose that you make the REST call for map information from the onMapReady() method itself. This way, you can be sure that when the callback is fired, there is an actual live Google map handle to which you could assign a marker etc.

Here is a skeleton of what the code might look like:

@OverridepublicvoidonMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    newYourAsyncTask(this).execute();
}

Then in your async task you can parse the data and assign the marker to your Google Map.

Solution 2:

I think you have to make a function for what ever task you want to perform on map basis on user experience and pass parameter related which value you need to perform your task on map like latitude longitude.

privatevoidyourFunctionName(double lat1, double lng1){
   LatLnglocation=newLatLng(Double.valueOf(lat1), Double.valueOf(lng1));
   CameraUpdateupdate= CameraUpdateFactory.newLatLngZoom(location, 15);
   mGoogleMap.moveCamera(update);
   MarkerOptionsoptions=newMarkerOptions()
        .position(location)
        .snippet("I'm around here!!!");
   CircleOptionscircle=newCircleOptions()
        .center(location)
        .radius(2000)
        .fillColor(0x55add8e6)
        .strokeColor(Color.BLUE)
        .strokeWidth(2);
   mGoogleMap.addCircle(circle);
   mGoogleMap.addMarker(options);
}

you not need to perform any task in onMapReady() function

@OverridepublicvoidonMapReady(GoogleMap googleMap) {
   mGoogleMap = googleMap;

}

store only google map object

mGoogleMap = googleMap;

now perform network operation and call which function you want to perform

HashMap<String, String> postData = newHashMap<>();
    postData.put("rqstID", rqstID);
    AsyncClass taskDetails = newAsyncClass(AcceptActivity.this, postData, 
    "Getting details", newAsyncResponse() {
        @OverridepublicvoidprocessFinish(String s) {
            Log.d("AcceptActivity", s);
            JSONObject parentObject = null;
            try {
                parentObject = newJSONObject(s);
                JSONArray parentArray = parentObject.getJSONArray("result");
                JSONObject finalObject = parentArray.getJSONObject(0);
                lat1 = finalObject.getString("rqstLat");
                lng1 = finalObject.getString("rqstLng");
                yourFunctionName(Lat1, lng1) // CALL THIS AFTER GETED RESULT
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    taskDetails.execute("http://10.0.2.2/wingman/requestDetails.php");

Solution 3:

Adding Async in onMapReady() won't help, instead replace somefragment.getMapAsync(this) with your Async task call and add somefragment.getMapAsync(this) in the end of onPostExecute.

Post a Comment for "Android Studio Execute Asyncclass Before Onmapready"