JS:如何制作动态的Google Maps Polyline?

时间:2013-10-08 12:33:25

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

我正在制作一张需要显示的Google地图: 一个KML(平面图) 一条折线应该从GET响应中获取坐标,每5秒钟一次。 我希望折线能够使用从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();
})

但我什么都没发生。我也没有错。 有人可以帮助我吗? 感谢..

1 个答案:

答案 0 :(得分:0)

两个问题:

  1. 必要的var,Path1initialize()的内部和私有,因此超出了与ajax成功函数相关的范围。

    < / LI>
  2. ajax success函数除了将从响应派生的字符串推送到数组之外什么都不做。这样做本身不会影响折线。

  3. 首先修复(1),然后修复(2)。