【问题标题】:JS: How to make a dynamic Google Maps Polyline?JS:如何制作动态的谷歌地图折线?
【发布时间】:2013-10-08 12:33:25
【问题描述】:

我正在制作需要显示的 Google 地图: 一个 KML(平面图) 一条折线,每 5 秒从 GET 响应中获取其坐标。 我希望折线使用来自 RESTful API 的新坐标进行自我更新。 这是代码[更新]:

        var FlightPath1 = []
            $(document).ready(function() {
            var BASE_URL = "https://its.navizon.com/api/v1/sites/"  //Do not change this
            SITE_ID = "1001"  // Your site ID here
            MAC_add = "00:1E:8F:92:D0:56"  //Mac address of the device to track
            USERNAME = "demo@navizon.com"  // Your username
            PASSWORD = ""  // Your password
            var Path1=new google.maps.Polyline({
                path:FlightPath1,
                strokeColor:"#F020FF",
                strokeOpacity:0.8,
                strokeWeight:2
                });
// Send the request
            jQuery.support.cors = true;   // Enable cross-site scripting
function makeCall() {
    $.ajax({
        type: "GET",
        url: BASE_URL + SITE_ID + "/stations/" + MAC_add + "/",
        beforeSend: function(jqXHR) {
        jqXHR.setRequestHeader("Authorization", "Basic " + Base64.encode(USERNAME + ":" + PASSWORD));
        },

        success: function(jimmi) {
            // Output the results
            if (typeof jimmi === "string") {
                jimmi = JSON.parse(jimmi);
            }
            //Display the results
                FlightPath1.push("new google.maps.LatLng(" + jimmi.loc.lat + "," + jimmi.loc.lng + "),");
                var mapOptions = {
                                    zoom: 19,
                                    center: new google.maps.LatLng(jimmi.loc.lat,jimmi.loc.lng),
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                                };

                                map = new google.maps.Map(document.getElementById('map-canvas'),
                                mapOptions);

                                var SanDiegoKML = new google.maps.KmlLayer({
                                url: 'https://s3.amazonaws.com/navizon.its.fp/1001/05w0kyw829_a.kml'
                                });
                                SanDiegoKML.setMap(map);
                                google.maps.event.addDomListener(window, 'load', jimmi);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('Error');
        }
    });
    window.setTimeout(makeCall, 5000); //run the script each 5000 milliseconds
}
makeCall();
})

但我什么也没发生。而且我也没有错误。 有人可以帮我吗? 谢谢..

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 get google-polyline


    【解决方案1】:

    两个问题:

    1. 必要的变量Path1 是initialize() 的内部和私有变量,因此超出了完全不同范围内的ajax 成功函数的范围。

    2. ajax 成功函数除了将从响应中派生的字符串压入数组外,什么都不做。这样做本身不会影响折线。

    先修复 (1),然后修复 (2)。

    【讨论】:

      猜你喜欢
      • 2013-05-23
      • 2015-08-05
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2016-05-14
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多