1
我是Google Map應用程序的新手。我已閱讀關於Polyline數組的文章(https://developers.google.com/maps/documentation/javascript/overlays#PolylineArrays)。在這個例子中,它使用Event Click創建了一個動態數組。我的問題是,我可以創建一個數組變量而不是單擊事件中的多段線嗎?Google Map折線陣列
下面是從谷歌文檔代碼:
var poly;
var map;
function initialize() {
var chicago = new google.maps.LatLng(41.879535, -87.624333);
var mapOptions = {
zoom: 7,
center: chicago,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3
}
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
// Add a listener for the click event
google.maps.event.addListener(map, 'click', addLatLng);
}
/**
* Handles click events on a map, and adds a new point to the Polyline.
* @param {MouseEvent} mouseEvent
*/
function addLatLng(event) {
var path = poly.getPath();
// Because path is an MVCArray, we can simply append a new coordinate
// and it will automatically appear
path.push(event.latLng);
// Add a new marker at the new plotted point on the polyline.
var marker = new google.maps.Marker({
position: event.latLng,
title: '#' + path.getLength(),
map: map
});
}
我做了我自己的一些代碼:
<script>
var line;
var map;
function initialize() {
var mapDiv = document.getElementById('map-canvas');
map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(0, -180),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var path = [new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856)];
line = new google.maps.Polyline({
path: path,
strokeColor: '#ff0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
line.setMap(map)
}
function addNewPoint(position) {
var path = line.getPath();
path.push(position);
}
''''' I create a single variable for a new path.
var NewPath = [new google.maps.LatLng(-27.46758, 153.027892)];
addNewPoint(NewPath);
google.maps.event.addDomListener(window, 'load', initialize);
</script>
當我運行它。這是行不通的。謝謝你的回覆。
謝謝你的友好迴應鄧肯。我已經改變了它。我正在嘗試使用數組變量添加新的折線。我從谷歌獲得的唯一例子是它只使用click事件添加折線。是否有可能只是使用數組變量來添加新的多段線? – user2000771
謝謝你對鄧肯的友善迴應。我已經改變了它。我正在嘗試使用數組變量添加新的折線。我從谷歌獲得的唯一例子是它只使用click事件添加折線。是否有可能在初始化後使用數組變量添加新的多段線?我的目標是,我將從我的數據庫SQLServer中獲得Lat和Long折線。並不時使用定時器setInterval來實現它。感謝您的任何指導。 – user2000771
是的,您可以添加多段線而不僅僅是響應用戶點擊。要從SQL服務器獲取它,需要通過服務器端頁面提供信息,您可以通過Ajax調用該頁面 – duncan