在Google Maps API V3中使用LatLng坐标数组绘制折线

Drawing PolyLine Using an Array of LatLng Coordinates in Google Maps API V3

本文关键字:坐标 LatLng 数组 绘制 折线 Maps Google API V3      更新时间:2023-09-26

我试图在Javascript中使用谷歌地图API v3绘制动态折线,这是我使用的代码片段:

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myTrip=new Array();
//var y=new google.maps.LatLng(28.360012,77.031527);
//var z=new google.maps.LatLng(28.360124,77.031429);
myTrip.push(new google.maps.LatLng(28.360012,77.031527));
myTrip.push(new google.maps.LatLng(28.360124,77.031429));
myTrip.push(new google.maps.LatLng(28.361024,77.034129));
var flightPath=new google.maps.Polyline({
path:myTrip,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2
});
flightPath.setMap(map); 
google.maps.event.addDomListener(window, 'load', initialize);

这不是在地图上绘制折线。

有人能帮我一下吗?

您缺少一个initialize功能:

function initialize() {
    var mapProp = {
        center: new google.maps.LatLng(28.360012, 77.031527),
        zoom: 18,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
    var myTrip=new Array();
    myTrip.push(new google.maps.LatLng(28.360012,77.031527));
    myTrip.push(new google.maps.LatLng(28.360124,77.031429));
    myTrip.push(new google.maps.LatLng(28.361024,77.034129));
    var flightPath=new google.maps.Polyline({
        path:myTrip,
        strokeColor:"#0000FF",
        strokeOpacity:0.8,
        strokeWeight:2
    });
    flightPath.setMap(map); 
}
google.maps.event.addDomListener(window, 'load', initialize);