我已经构建了一个网络应用程序,它使用google maps v3 API构建一个方向多个航点的地图。
jQuery.getJSON(driverURL, function(dData){
var routeObject = {};
var lat = dData.Location.lat;
var lng = dData.Location.lng;
routeObject.origin = new google.maps.LatLng(lat, lng);
routeObject.destination = new google.maps.LatLng(endRoute.lat, endRoute.lng);
routeObject.waypoints = waypoints;
routeObject.travelMode = google.maps.TravelMode.DRIVING;
routeObject.optimizeWaypoints = true;
directionsService.route(routeObject, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
var directionsDisplay = new google.maps.DirectionsRenderer({
polylineOptions: {
strokeColor: colors[driverLines.length]
}
});
directionsDisplay.setMap(dMap);
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
});
我已经存储了地图,航路点,起点和终点,但我希望能够生成一个谷歌地图可点击链接,指向我可以发送给用户的此路线的路线。我可以找到如何使用单个标记发送链接,但不知道如何使用完整的路径方向生成地图链接。
任何帮助表示感谢。
答案 0 :(得分:0)
我想出来了,它实际上比我想象的要容易,但还是做了一些。
首先,我将一些路点作为地址,而不是lat / lng点,并在maps.google.com上构建路线图。然后我查看了该地图的URL是如何构建的。重新创建非常简单。
网址只是http://maps.google.com/dir/starting地址,地址,城市,州,邮编/航点作为地址,城市,州,邮编/航点/结束地址作为地址,城市,州,邮编和您的地址完成。如果您为起点,航点,终点位置使用lat / lng坐标,则必须先使用V3 api对它们进行地理编码,然后从返回的结果中提取地址信息并将其插入到您的URL中,但它确实有效。
这里是我如何制作一个有一个开始,一堆航路点和一个结束位置的最终链接。我从一个名为Data的JSON对象开始,该对象包含所有地址信息,并以Google Maps期望的格式构建一个长字符串:
var dirs = ''
for(var x = 0; x<Data.length;x++){
dirs += Data[x].Address + "," + Data[x].City + "," + Data[x].State + "," + Data[x].Zip + "/";
}
然后,我将开始和结束位置添加到它:
var dirLink = 'http://maps.google.com/maps/dir/';
dirLink += start.Address + "," + start.City + "," + start.State + "," + start.Zip + "/" + dirs + end.Address + "," + end.City + "," + end.State + "," + end.Zip;
dirLink将是一个完整的链接,它将从start.Address到所有航点指向end.Address。
答案 1 :(得分:0)
function initMap() {
var pointA = new google.maps.LatLng(51.7519, -1.2578),
pointB = new google.maps.LatLng(51.509865, -0.118092),
pointC = new google.maps.LatLng(50.8429, -0.1313),
myOptions = {
zoom: 7,
center: pointA
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
// Instantiate a directions service.
directionsService = new google.maps.DirectionsService,
directionsDisplay = new google.maps.DirectionsRenderer({
map: map
}),
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "A",
map: map
}),
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "B",
map: map
}),
markerC = new google.maps.Marker({
position: pointC,
title: "point C",
label: "C",
map: map
});
// get route from A to B
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, pointC);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, pointC) {
var first = new google.maps.LatLng(51.509865, -0.118092); //we can get them from params.
var request = {
origin: pointA,
destination: pointC,
waypoints: [{location: first, stopover: false}
], //here array of waypoints
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.WALKING
};
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.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
summaryPanel.innerHTML += route.legs[i].start_address + " to ";
summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";
}
} else {
alert("directions response " + status);
}
});
}
initMap();