0

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!

Pablo Pantaleon
  • 1,506
  • 1
  • 19
  • 38
  • When you re attach the fragment, the fragment it be created once again, so you need to save your markers into the bundle(maybe?) and repaint them – Danyel Cabello Feb 24 '15 at 20:01
  • You are saying store markers like this: http://stackoverflow.com/questions/16236439/restoring-map-state-position-and-markers-of-google-maps-v2-on-rotate-and-on? But i only want to create them again, i don't want store state – Pablo Pantaleon Feb 24 '15 at 20:14
  • There is a method called when the fragment is de-attached onSaveInstanceState http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle) – Danyel Cabello Feb 24 '15 at 20:24
  • But there's a problem, because you are saying that i need to use onSaveInstanceState to store for example the makers position. But the thing is that i fetch from server the markers so i don't to store it because i need fresh data. So the outState.putDouble("LAT", mMap.getCameraPosition().target.latitude); for example doesn't work in this case. Because i need fresh data. Thanks for replying – Pablo Pantaleon Feb 24 '15 at 21:17
  • Then... I suppose that in the fragment on the onCreate, onCreateView or onActivityCreated you need to request the fresh data... – Danyel Cabello Feb 24 '15 at 23:22
  • Nope, that doesn't work. It's the same output. Another solution? – Pablo Pantaleon Feb 25 '15 at 17:37
  • I have same problem in my app. @PabloReyes If you got the solution then please write in the answer. Thank you – Ekta Bhawsar Nov 14 '16 at 10:16
  • Sorry. I remember that i've solved the issue, but i don't remember how :P, also i'm not really sure in which project i solved it, but if i find it, i will reply. – Pablo Pantaleon Nov 14 '16 at 20:27
  • Ok, I didn't get any solution yet, but thank you in advance :( – Ekta Bhawsar Nov 16 '16 at 09:39

0 Answers0