我想在Google地图上渲染多条路线,例如,如果我点击路线A点的标记,它会渲染此路线,如果我点击另一个标记,它将绘制另一条路线,I尝试这样做,但它远离加载相同的路线,在我的数据库我有多个ID,我想用来定义我将渲染的路线。
所以,我会发布我的示例和我的代码给你理解。
这是我的loadproject脚本:
var map, ren, ser;
var data = {};
var data2 = {};
var directionsDisplay;
var directionResult = [];
var rotaArray = [];
var directionsService = new google.maps.DirectionsService();
var infowindow;
function goma()
{
map = new google.maps.Map( document.getElementById('mappy'), {'zoom':12, 'mapTypeId': google.maps.MapTypeId.ROADMAP, 'center': new google.maps.LatLng(-23.611654, -46.700329)});
var infoWindow = new google.maps.InfoWindow;
ren = new google.maps.DirectionsRenderer( {'draggable':true} );
ren.setMap(map);
ren.setPanel(document.getElementById("directionsPanel"));
ser = new google.maps.DirectionsService();
downloadUrl("rotasXML.php", function(data) {
var rotasXML = data.responseXML;
var rotas = rotasXML.documentElement.getElementsByTagName("rota");
for (var i = 0; i < rotas.length; i++ )
{
var id = rotas[i].getAttribute("id");
var latA = parseFloat(rotas[i].getAttribute("latA"));
var lngA = parseFloat(rotas[i].getAttribute("lngA"));
var latB = parseFloat(rotas[i].getAttribute("latB"));
var lngB = parseFloat(rotas[i].getAttribute("lngB"));
var rotaB = new google.maps.LatLng(
parseFloat(rotas[i].getAttribute("latB")),
parseFloat(rotas[i].getAttribute("lngB")));
var rotaA = new google.maps.LatLng(latA,lngA);
var html = "<b> Marker " + id + "</b> <br/>",
html = html + "FROM "+latA+" : "+lngA;
html = html + "<br/>To "+latB +" : "+lngB;
var parameter = id+","+latA+","+lngA+","+latB+","+lngB;
html = html + "<br/><button onclick='doIt("+id+ latA+ latB+ lngA+ lngB+")'>Load Route</button>";
var wayA = new google.maps.Marker({
map: map,
position: rotaA
});
rotaArray.push(wayA);
bindInfoWindow(wayA, map, infoWindow, html);
}
});
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url2, callback) {
var jax = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
jax.onreadystatechange = function()
{
if (jax.readyState == 4) {
jax.onreadystatechange = doNothing;
callback(jax, jax.sts);
}
};
jax.open('GET', url2, true);
jax.send(null);
}
function doIt(id,latA, latB, lngA, lngB){
alert(id);
ser.route({
'origin':new google.maps.LatLng(latA, lngA),
'destination':new google.maps.LatLng(latB, lngB),
'travelMode': google.maps.DirectionsTravelMode.DRIVING},
function(response){
ren.setDirections(response);
});
}
function setAllMap(map) {
for (var i = 0; i < rotaArray.length; i++) {
rotaArray[i].setMap(map);
}
}
function clearOverlays() {
setAllMap(null);
}
function deleteOverlays() {
clearOverlays();
rotaArray = [];
}
}
我想点击wayPoint并渲染正确的路线
答案 0 :(得分:1)
要启用选择标记以显示路线,您需要数组来保存标记
var markerArray = [];
路线坐标
var routeArray =[[],[],[],[],[]];
以下是加载标记的代码
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
alert(markers.length);
for (var i = 0; i < markers.length+1; i++) {
id = markers[i].getAttribute("id");
var latA = parseFloat(markers[i].getAttribute("latA"));
var lngA = parseFloat(markers[i].getAttribute("lngA"));
var latB = parseFloat(markers[i].getAttribute("latB"));
var lngB = parseFloat(markers[i].getAttribute("lngB"));
//fill routeArray
routeArray[i].push(id);
routeArray[i].push(latA);
routeArray[i].push(lngA);
routeArray[i].push(latB);
routeArray[i].push(lngB);
var point = new google.maps.LatLng(latA,lngA);
//Build html for Infowindow
var html = "<b> Marker " + id + "</b> <br/>",
html = html + "FROM "+latA+" : "+lngA;
html = html + "<br/>To "+latB +" : "+lngB;
var parameter = id+","+latA.toString()+","+lngA.toString()+","+latB.toString()+","+lngB.toString();
html = html + "<br/><button onclick='doIt("+id+")'>Load Route</button>";
var marker = new google.maps.Marker({
map: map,
position: point,
});
//marker array to allow clearin markers
markerArray.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
}
}
加载路由的代码(从doIt()调用)
function calcRoute(latAa,lngAa,latBa,lngBa) {
var start = new google.maps.LatLng(latAa,lngAa);
var end = new google.maps.LatLng(latBa,lngBa);
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById("directions_panel");
summaryPanel.innerHTML = "";
// For each route, display summary information.
i = 0;
summaryPanel.innerHTML += "From "+route.legs[i].start_address + " <br /> ";
summaryPanel.innerHTML += "To "+route.legs[i].end_address + "<br />";
summaryPanel.innerHTML += "Distance: "+route.legs[i].distance.text + "<br />";
distance = parseFloat(route.legs[i].distance.text)*rate;
}
});
}
function doIt(id){
//Convert for array zero index
id = id-1;
var latAa= routeArray[id][1];
var lngAa= routeArray[id][2];
var latBa= routeArray[id][3];
var lngBa= routeArray[id][4];
clearOverlays();
calcRoute(latAa,lngAa,latBa,lngBa);
}
此DEMO显示结果
您必须相应地修改代码。注意还有其他功能可以清除标记,可以在源
中看到