Question

This is my problem...it may be simple but i'm stuck:

Context

I have some informations that i downloaded from Internet (text and small imgs), and i want to display it inside the bubble when i tap inside myMarkers class. I want that this work both landscape and not.

Problem

When the app is portrait and when i kill the application and i re-run it, all is working (when i tap i see the bubble with text and img). When i turn to landscape and when my app is not visible but alive, the map restore markers but when i tap on them, they doesn't display the bubble.

Code

Here my implementation of Marker that implements Parcelable in order to save them into the Bundle when i call onSaveInstanceState when my Activity go onPause() (ommitted empty override methods) :

class myMarker extends Marker implements Parcelable{

            public myMarker(MapView mapView) {
                    super(mapView);
            }

            public  Parcelable.Creator<myMarker> CREATOR = new Parcelable.Creator<myMarker>() {

            public myMarker createFromParcel(Parcel in) {
                return new myMarker(map);
            }

            public myMarker[] newArray(int size) {
                return new myMarker[size];
            }
            };

}

Inside the onCreate() in the Activity we have:

 if (savedInstanceState != null) {
    // Restore myMarkers
    markers = savedInstanceState.getParcelableArrayList("MARKERS");
    for (int i=0;i<markers.size();i++){                  
         map.getOverlays().add(markers.get(i));
         Marker m=markers.get(i);

            }
         map.invalidate();
        } 
 else {
    markers=new ArrayList<myMarker>();
        }

And the saving process:

@Override
      public void onSaveInstanceState(Bundle savedInstanceState) {
          // Save the user's current state
          savedInstanceState.putParcelableArrayList("MARKERS", markers);

          // Always call the superclass so it can save the view hierarchy state
          super.onSaveInstanceState(savedInstanceState);
      }

What am i doing wrong? I'm missing something? Thanks

Was it helpful?

Solution

I resolved

There is an issue i think. Every time that you turn the device on landscape, the Marker's MarkerInfoWindow has an old reference to the Context and that's why the bubble is not shown on click. (maybe can be fixed using fragments...)

Just rewrite the class and when recreate the markers you just pass the new reference to the context.

The code:

public class myMarker extends Marker implements Parcelable{
    InfoWindow i;
    MapView map;
    static Context context=null;
    static MarkerInfoWindow ma=null;



    public myMarker(MapView mapView) {
    super(mapView);
    Context c=mapView.getContext();
   /*IF THE CONTEXT CHANGES, I KEEP MY NEW (RECREATED) mapView AND I SET A NEW static 
   MarkerInfoWindow OTHERWISE AT ANY CASE I SET THE static markerInfoWindow TO THE MARKER */
    if(context==null||context!=c){
    i=super.mInfoWindow;
    map=mapView;
    context=c;
    String packageName=context.getPackageName();
    int layoutResId=context.getResources().getIdentifier("layout/bonuspack_bubble",null,packageName);
    ma=new MarkerInfoWindow(layoutResId, mapView);
    }
    super.setInfoWindow(ma);


}
...
}

And the onRestore, when you reload the map in landscape, after onSave:

/*@ onRestore i keep my old markers and i create new markers with same info but with new mapView(new Context)*/
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState){                
      super.onRestoreInstanceState(savedInstanceState);

              if (savedInstanceState != null) {


                  /*NEW MARKERS EQUALS TO THE OLD BUT WITH NEW MAP (NEW CONTEXT)
                  ...AND NO YOU CANT JUST UPDATE THE CONTEXT*/

                   ArrayList<myMarker> markers2;
                    markers2 = savedInstanceState.getParcelableArrayList("MARKERS");

                    if(markers2!=null){
                   for (int i=0;i<markers2.size();i++){
                       myMarker m=new myMarker(map);
                       m.setTitle(markers2.get(i).getTitle());
                       m.setImage(markers2.get(i).getImage());
                       m.setPosition(markers2.get(i).getPosition());
                       m.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
                       markers.add(m);

                            map.getOverlays().add(m);


                         }
                    }


                        map.invalidate();                  
             }

                else markers=new ArrayList<myMarker>();  




      }

OTHER TIPS

Well, I confirm, you discovered an issue in the OSMBonusPack Marker (I'm the developer) => The static MarkerInfoWindow mDefaultInfoWindow doesn't support MapView changes.

You found one example: changing orientation, and your constructor seems a good approach to fix it.

But there are other cases: having multiple MapView in the same application. More complex.

I will try to fix that issue directly inside the lib. You can follow the progress in the project Issues (issue #58).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top