2014-07-08 123 views
0

我試圖繪製使用谷歌地圖API V3在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); 

這不在地圖上繪製折線。

有人可以幫助我嗎?

+0

您的代碼工作正常,但不完整。拿着你的地圖的變量真的叫做'map'嗎?順便說一句。你不需要座標周圍的單引號。例如:'new google.maps.LatLng(28.360012,77.031527)' – MrUpsidown

+0

是的變量持有地圖是'地圖',我刪除了引號,但它不能正常工作。我在代碼 – user3815163

+0

中進行了更改什麼不起作用?你在瀏覽器的JavaScript控制檯中得到任何錯誤? – MrUpsidown

回答

2

你錯過了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); 
+0

感謝duncan,它工作得很好 – user3815163

相關問題