Search code examples
javascriptmapquest

How can I reload a mapquest map without refreshing page?


Im using the traffic layer that is available with mapquest, here is the function Im using:

    function displayTrafficMap(lati,long,trafficdiv) {

    var options={

            elt:document.getElementById(trafficdiv), 
            zoom:10,
            latLng:{lat: lati,lng: long}, 
            mtype:'map',
            bestFitMargin:0,
            zoomOnDoubleClick:true

    };

    window.map = new MQA.TileMap(options);

    MQA.withModule('zoomcontrol3','viewcontrol3','traffictogglecontrol', 'shapes', function()  {

        var ttc = new MQA.TrafficToggleControl();
        var vwc = new MQA.ViewControl3();
        var zc = new MQA.LargeZoomControl3();

        window.map.addControl( zc , new MQA.MapCornerPlacement( MQA.MapCorner.TOP_RIGHT,  new MQA.Size(5,40)));
        window.map.addControl( vwc , new MQA.MapCornerPlacement(MQA.MapCorner.TOP_RIGHT));
        window.map.addControl( ttc , new MQA.MapCornerPlacement(MQA.MapCorner.TOP_RIGHT));

        ttc.toggle();

    });

}

When the page loads I load this function like so:

<script>window.onload = function() { displayTrafficMap(latitude,longitude,'trafficmap'); }</script>

This works fine, but how do I call this function again with new latitude and longitude without having to refresh the page? I tried this:

<a href="#" onclick="displayTrafficMap(newlat,newlon,'trafficmap');return false;">

But nothing happens. Any suggestions?


Solution

  • It appears my answer was lacking, hopefully this will provide more information on the solution to the question. To confirm the question, I am using mapquest to display a map with traffic conditions on it. How do I update the maps lat/lon without having to refresh the page? Below is the entire script I use, including using jquery, hope this gives more of the answer someone might be looking for:

    var traffic_map;
    
    function displayTrafficMap(lati,long) {
    
        var options={
    
                elt:document.getElementById('trafficmap'), 
                zoom:10,
                latLng:{lat: lati,lng: long}, 
                mtype:'map',
                bestFitMargin:0,
                zoomOnDoubleClick:true
    
        };
    
        traffic_map = new MQA.TileMap(options);
    
        MQA.withModule('zoomcontrol3','viewcontrol3','traffictogglecontrol', 'shapes', function()  {
    
            var ttc = new MQA.TrafficToggleControl();
            var vwc = new MQA.ViewControl3();
            var zc = new MQA.LargeZoomControl3();
    
            traffic_map.addControl( zc , new MQA.MapCornerPlacement( MQA.MapCorner.TOP_RIGHT,  new MQA.Size(5,40)));
            traffic_map.addControl( vwc , new MQA.MapCornerPlacement(MQA.MapCorner.TOP_RIGHT));
            traffic_map.addControl( ttc , new MQA.MapCornerPlacement(MQA.MapCorner.TOP_RIGHT));
    
            ttc.toggle();
    
        });
    
    }
    
    function changeTrafficMap(lati,long)
    {
    
        traffic_map.setCenter({lat: lati,lng: long});
    
    }
    
    //We run the display map function right away to load the map
    displayTrafficMap(trafficlat,trafficlon);
    
    $(document).ready(function() {
    
        $('#traffic_areas').bind('change', function(ev) {
    
            //We get the id for the area that was selected
            var areaid = $(this).val();
    
            $.ajax({
    
                url:"/traffic.php?aid=" + areaid,
                type:"GET",
                dataType:"json",
                success: function(data) {
    
                    //First we change the current map view location
                    changeTrafficMap(data.lat,data.lon);
    
                }
    
            });
    
        });
    
    });
    

    The ajax call is calling a php script called traffic.php, that script connects to my database to grab the lat/lon I assigned to the areaid that is being passed via the drop down selection.