1
我試圖在谷歌地圖上畫一條線,我試試這個代碼,沒有發生任何事情,只有標記沒有線,我已經讀了一些參考像https://developers.google.com/maps/documentation/javascript/v2/overlays#Icons_overview 並嘗試代碼,但行仍然不出來,任何人都可以幫助我?下面 是我的代碼,假設地址是陣列在谷歌地圖中畫線
for (var i = 0; i < address.length; i++) {
geocoder.geocode({ 'address': address[i] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (i == 2) {
var pos1 = results[0].geometry.location;
alert(pos1);
}
else if (i == 2) {
var pos2 = results[0].geometry.location;
alert(pos2);
}
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker
(
{
position: results[0].geometry.location,
map: map,
title: 'click me'
}
);
var infowindow = new google.maps.InfoWindow
(
{
content: '<img src="http://wallpaperscraft.com/image/child_girl_emotion_sweet_face_25641_100x100.jpg"> Welcome to origineit'
}
);
var flightPlanCoordinates = [
new google.maps.LatLng(pos1),
new google.maps.LatLng(pos2)
];
if (i == 2) {
var polyline = new google.maps.Polyline
({
path: flightPlanCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3
});
polyline.setMap(map);
}
google.maps.event.addListener(marker, 'click', function() {
// Calling the open method of the infoWindow
infowindow.open(map, marker);
});
}
else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
我已經按照你的意見,但它仍然沒有工作 – Franky
是的我現在看到你也有一些範圍界定問題。 pos1和pos2將永遠不會同時存在,因爲它們都在內部函數中聲明,但設置在不同的循環中。我會更新我的答案,以適應這一點。 –
這段代碼有很多問題,所以我改變了一些其他的東西(例如,由於你的解決方案總是檢查它是否是兩個(它總是),所以我放棄了對'i'的依賴關係,例如有缺陷)。希望這可以幫助! –