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



我试图在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);

最新更新