简体   繁体   English

如何在不刷新页面的情况下重新加载mapquest地图?

[英]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: 我正在使用mapquest可用的流量层,以下是我正在使用的功能:

    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? 有什么建议么?

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. 为了确认这个问题,我正在使用mapquest来显示一个带有交通状况的地图。 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: 以下是我使用的整个脚本,包括使用jquery,希望它能提供更多有人可能正在寻找的答案:

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. ajax调用正在调用一个名为traffic.php的php脚本,该脚本连接到我的数据库以获取我分配给通过下拉选择传递的areaid的经/纬度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM