Issue
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 always null. I call the asynctask on my oncreate here is my code.
AsyncTask on onCreate
HashMap<String, String> postData = new HashMap<>();
postData.put("rqstID", rqstID);
AsyncClass taskDetails = new AsyncClass(AcceptActivity.this, postData, "Getting details", new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d("AcceptActivity", s);
JSONObject parentObject = null;
try {
parentObject = new JSONObject(s);
JSONArray parentArray = parentObject.getJSONArray("result");
JSONObject finalObject = parentArray.getJSONObject(0);
lat1 = finalObject.getString("rqstLat");
lng1 = finalObject.getString("rqstLng");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
taskDetails.execute("http://10.0.2.2/wingman/requestDetails.php");
onMapReady
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
LatLng location = new LatLng(Double.valueOf(lat1), Double.valueOf(lng1));
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(location, 15);
mGoogleMap.moveCamera(update);
MarkerOptions options = new MarkerOptions()
.position(location)
.snippet("I'm around here!!!");
CircleOptions circle = new CircleOptions()
.center(location)
.radius(2000)
.fillColor(0x55add8e6)
.strokeColor(Color.BLUE)
.strokeWidth(2);
mGoogleMap.addCircle(circle);
mGoogleMap.addMarker(options);
}
Solution
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:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
new YourAsyncTask(this).execute();
}
Then in your async task you can parse the data and assign the marker to your Google Map.
Answered By - Tim Biegeleisen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.