I've a trouble when i try to add Markers to GoogleMap
story: I open the app, then open the activity that have the Google map. The HTTP request was executed correctly and the markers are added perfectly without any problem. I can click each one, etc. But if i press back button, then entered again in the activity, all markers are missing... so i debugged trying to found if the method is called, and yes it called. Supposedly the markers are added (but i can't see them).
So only works when i open the activity by first time. When i open activity again ... markers missing.
Here is the code:
My Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
...
// setup map
final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.googleMap);
mapFragment.getMapAsync(this);
}
I have a listener, this method is called by AsyncTask (postExecute), made a Http Request and then received the jsonArray -> add markers
@Override
public void onRequestCompleted(final JSONArray json) {
// verified, json is correct
Log.i(Constant.TAG, "Ok! - " + json);
for (int i = 0; i < json.length(); i++) {
try {
// verified, place is correct
Place place = new Place(json.getJSONObject(i));
if (place != null) {
// Try with place
LatLng markerPosition = new LatLng(place.getLat(), place.getLon());
// setup marker
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title(place.getPlaceName());
markerOptions.position(markerPosition);
// Try with default marker (hardcoded)
LatLng markerPosition = new LatLng(14.64281,-90.51271);
// setup marker
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title("duka");
markerOptions.position(markerPosition);
mMap.addMarker(markerOptions)
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/googleMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
Because code above doesn't work, i try with only one marker. But is the same output. (open first time, it work. second time doesn't)
runOnUiThread(new Runnable() {
public void run() {
// add marker here... but doesn't work
// Try with default marker (hardcoded)
LatLng markerPosition = new LatLng(14.64281,-90.51271);
// setup marker
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title("duka");
markerOptions.position(markerPosition);
mMap.addMarker(markerOptions)
}
});
Thanks!