0
我實際繪製了具有從XML文件中解析出的多個標記的地圖。附加地,我在地圖上繪製了兩個Polyglons。 Coords在腳本中是硬編碼的,我也想從XML文件中加載polyglon coords。這是我的實際代碼:從XML文件在Google Maps API V3上繪製多個多邊形
<script type="text/javascript">
var customIcons = {
warning: {
icon: 'https://scoo.me/img/marker_warning.png'
},
activate: {
icon: 'https://scoo.me/img/marker_activate.png'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
scrollwheel: false,
mapTypeId: 'roadmap',
mapTypeControl: false
});
var infoWindow = new google.maps.InfoWindow;
downloadUrl("../xml/vehicles_service_required.php", function(data) {';
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
var VehicleID = markers[i].getAttribute("VehicleID");
var VehicleLabel = markers[i].getAttribute("VehicleLabel");
var ServiceRequired = markers[i].getAttribute("ServiceRequired");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
bounds.extend(point);
var html = "<b>Roller-Nr. " + VehicleLabel + "</b> <br/>(ID-" + VehicleID + ")";
var icon = 'https://scoo.me/img/marker_activate.png';
//var icon = {};
if (ServiceRequired == "0") {
icon = 'https://scoo.me/img/marker_warning.png';
} else if (ServiceRequired == "-1") {
icon = 'https://scoo.me/img/marker_activate.png';
}
var marker = new google.maps.Marker({
map: map,
position: point,
//icon: 'https://scoo.me/img/marker_warning.png'
icon: icon
});
bindInfoWindow(marker, map, infoWindow, html);
}
map.fitBounds(bounds);
// Polyglon Munich
var muenchen = new google.maps.Polygon({
paths: [
new google.maps.LatLng(48.163108,11.588816),
new google.maps.LatLng(48.169462,11.595339),
new google.maps.LatLng(48.173069,11.599073),
new google.maps.LatLng(48.176016,11.592807),
new google.maps.LatLng(48.182455,11.599373)],
strokeColor: '#21a9e1',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#21a9e1',
fillOpacity: 0.2,
map: map
});
// Geschäftsgebiet Cologne
var koeln = new google.maps.Polygon({
paths: [
new google.maps.LatLng(50.9558045,6.9741296),
new google.maps.LatLng(50.9567777,6.9755029),
new google.maps.LatLng(50.9579266,6.9736361),
new google.maps.LatLng(50.9574265,6.9724559),
new google.maps.LatLng(50.9596701,6.9679069),
new google.maps.LatLng(50.9600215,6.9656538)],
strokeColor: '#21a9e1',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#21a9e1',
fillOpacity: 0.2,
map: map
});
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
我的XML爲Polgon正在尋找這樣的:
<subdivisions>
<subdivision Name="Muenchen">
<coord lat="48.163108" lng="11.588816"/>
<coord lat="48.169462" lng="11.595339"/>
<coord lat="48.173069" lng="11.599073"/>
<coord lat="48.176016" lng="11.592807"/>
<coord lat="48.182455" lng="11.599373"/>
</subdivision>
<subdivision Name="Koeln">
<coord lat="50.9558045" lng="6.9741296"/>
<coord lat="50.9567777" lng="6.9755029"/>
<coord lat="50.9579266" lng="6.9736361"/>
<coord lat="50.9574265" lng="6.9724559"/>
<coord lat="50.9596701" lng="6.9679069"/>
<coord lat="50.9600215" lng="6.9656538"/>
<coord lat="50.9627245" lng="6.9615983"/>
<coord lat="50.9651706" lng="6.9538736"/>
<coord lat="50.9664139" lng="6.9517493"/>
</subdivision>
</subdivisions>
可有人請給我一個例子,如何實現XML在我的腳本polyglone。先謝謝你!
謝謝@Swatchai爲你的偉大的答覆。我只是想知道如何實現我的PHP文件,它在'var xmltext'中生成我的XML數據(示例:https://scoo.me/test_tmp/test.php)。這應該用_'downloadUrl(「https://scoo.me/test_tmp/test.php」,函數(數據)'_? –
實現並且我必須將代碼放在我的腳本中? –