Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Using MapView in different windows

I'm working on an app that has a need for a MapView to be used in multiple windows throughout. It works fine on the iPhone, but on Android there is a stack trace error about only having one MapView per application.

Ok, so only one MapView. I should be able to create the MapView in the app.js file and pass it around (clearing/creating annotations as I go), but when I do that the MapView loads a single time, but no where else.

Has anyone created anything on Android using MapViews in multiple locations? If so, how?

— asked June 11th 2010 by Joe K
  • android
  • map
  • mapview
  • mobile
1 Comment
  • I really need to know answer to this as well.. new mutiple usage of MapVIew, it seems imposible in Android..

    Please help

    — commented August 9th 2010 by Petr Cervenka

2 Answers

  • I have found this solution:

    myMap.View = (function() {
        var singletonMap = null;
        var api = {};
        api.removeAllAnnotations = function() {
            while (singletonMap.annotations.length) {
                singletonMap.removeAnnotation(singletonMap.annotations[0]);
                singletonMap.annotations.shift();
            }
        };
        api.addAnnotation = function(a) {
            if (singletonMap != null) {
                singletonMap.addAnnotation(a);
                singletonMap.annotations.push(a);
            }
        };
        api.createMap = function(_args) {
            /* not existsa => create it */
            if(singletonMap == null) {
                singletonMap = Titanium.Map.createView(_args);
                singletonMap.annotations = [];
            } else {
                this.removeAllAnnotations();
                //may be other implementation
                for(var key in _args) {
                    if(_args.hasOwnProperty(key)) {
                        singletonMap[key] = _args[key];
                    }
                }
            }
            return singletonMap;
        }
        return api;
    })();
    
    /*
     to call it:
     myMap.View.createMap(_args);
     */
    
    — answered April 12th 2012 by Rainer Schleevoigt
    permalink
    0 Comments
  • For everyone that is struggling with this, essentially you will need to create a global instance of the mapview in Android, and then use that single instance throughout your application. This means:

    • Adding and removing the mapview from the window/view so at there is at most one window/view using the mapview at a time
    • Keeping a single javascript execution context throughout your application (otherwise map tiles will not refresh, and other weird bugs will happen from jumping execution contexts)

    I have to say that, even then, Android mapview experience is not the best in my experience. Sometimes when I open my app, all the mapview tiles show up, and other times (without doing any changes at all), the tiles for outside starting area do not show up.

    — answered November 9th 2011 by Johnny Wong
    permalink
    2 Comments
    • Johnny,
      Sorry to hijack thread but having same issue and thought you may e able to help.. Any help is much appreciated!!!!

      I am essentially doing the same thing, creating a global instance of a mapview and using it in 2 different places. The problem Im having is I open the first page, touch the mapview everything is ok. go back then to the second page touch the mapview and it blows up with :

      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866): (main) [704,880725] Sending event: exception on thread: main msg:android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@4064db80 is not valid; is your activity running?; Titanium 1.7.5,2011/11/02 17:00,ab20af7
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@4064db80 is not valid; is your activity running?
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at android.view.ViewRoot.setView(ViewRoot.java:562)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at android.view.Window$LocalWindowManager.addView(Window.java:433)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at android.widget.ZoomButtonsController.setVisible(ZoomButtonsController.java:370)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at com.google.android.maps.MapView.displayZoomControls(MapView.java:1053)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at com.google.android.maps.MapView$1.onDown(MapView.java:341)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:488)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at android.view.View.dispatchTouchEvent(View.java:3932)
      11-23 15:55:17.850: ERROR/TiUncaughtHandler(26866):     at 
      Blah
      Blah
      Blah
      Blah
      

      Essentially my code looks like the following:
      app.js

      LT.AndroidMapview = Ti.Map.createView({
              mapType : Ti.Map.STANDARD_TYPE,
              animate : true,
              width : '100%',
              height : '100%',
              regionFit : true
          });
      

      first page

      holderMapView.add(LT.AndroidMapview);
      win.add(holderMapView);
      
      //remove from view when closing window
      win.addEventListener('close', function(e)
          {
          holderMapView.remove(LT.AndroidMapview);
          });
      

      second page

      holderMapView2.add(LT.AndroidMapview);
      win.add(holderMapView2);
      
      //remove from view when closing window
      win.addEventListener('close', function(e)
          {
          holderMapView2.remove(LT.AndroidMapview);
          });
      

      — commented November 23rd 2011 by Andrew Royce
    • hi,

      do you have any solution for this problem?

      thanks,
      Eliza

      — commented March 18th 2012 by Eliza Sapir
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.