Search code examples
javascriptmapquest

How do I get a reference to a mapquest map from outside the script where it's defined?


I'm maintaining some code that involves the mapquest map API. There's a javascript init() function which has this line:

map = new MQA.TileMap(document.getElementById('map'),6,{lat:34, lng:-118},'hyb'); 

which sets up the map in a down the page with the id of 'map', as per the API.

My problem is that I want to be able to access this map from outside this function, but I can't seem to find anything in the mapquest API about getting the map object from the div it's contained in. Trying to call map-related methods on the result of document.getElementById("map") just doesn't work.


Solution

  • Sounds like you would use the global variable map to reference it.

    var map;
    function setUp(){
        map = new MQA.TileMap(document.getElementById('map'),6,{lat:34, lng:-118},'hyb');
    }
    
    function doSomething(){
        if(!map) return;
        map.XXX();  //where XXX is the method you want to call
    }