[英]how to draw a route between two markers in google maps
Hi I am trying to draw a route map between two markers using javascript.嗨,我正在尝试使用 javascript 在两个标记之间绘制路线图。 I have tried various examples found online but my map is not loading while trying out different examples.我尝试了网上找到的各种示例,但在尝试不同示例时,我的地图没有加载。 I am unable to figure out the cause of the error.我无法找出错误的原因。 My map just does not load.我的地图无法加载。
I am trying to draw a route for the below two markers.我正在尝试为以下两个标记绘制路线。
<script>
function mapLocation() {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(37.334818, -121.884886);
var mapOptions = {
zoom: 7,
center: chicago
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = new google.maps.LatLng(37.334818, -121.884886);
var end = new google.maps.LatLng(38.334818, -181.884886);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
}
mapLocation();
</script>
Can someone please help me draw a route between the two markers?有人可以帮我在两个标记之间画一条路线吗?
Two comments:两条评论:
Issues:问题:
if (status == "OK")
test to see that.在 else 情况下为if (status == "OK")
测试添加错误处理以查看。function mapLocation() {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(37.334818, -121.884886);
var mapOptions = {
zoom: 7,
center: chicago
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
google.maps.event.addDomListener(document.getElementById('routebtn'), 'click', calcRoute);
}
function calcRoute() {
var start = new google.maps.LatLng(37.334818, -121.884886);
//var end = new google.maps.LatLng(38.334818, -181.884886);
var end = new google.maps.LatLng(37.441883, -122.143019);
var bounds = new google.maps.LatLngBounds();
bounds.extend(start);
bounds.extend(end);
map.fitBounds(bounds);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
directionsDisplay.setMap(map);
} else {
alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
}
mapLocation();
working code snippet:工作代码片段:
function mapLocation() { var directionsDisplay; var directionsService = new google.maps.DirectionsService(); var map; function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); var chicago = new google.maps.LatLng(37.334818, -121.884886); var mapOptions = { zoom: 7, center: chicago }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); directionsDisplay.setMap(map); google.maps.event.addDomListener(document.getElementById('routebtn'), 'click', calcRoute); } function calcRoute() { var start = new google.maps.LatLng(37.334818, -121.884886); //var end = new google.maps.LatLng(38.334818, -181.884886); var end = new google.maps.LatLng(37.441883, -122.143019); var request = { origin: start, destination: end, travelMode: google.maps.TravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); directionsDisplay.setMap(map); } else { alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status); } }); } google.maps.event.addDomListener(window, 'load', initialize); } mapLocation();
html, body, #map-canvas { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <input type="button" id="routebtn" value="route" /> <div id="map-canvas"></div>
Quite a few mistakes.错误不少。 First is the geolocation.首先是地理位置。 Your second location is wrong as the longitude can only be from +180 to -180 so -181 doesn't exist in the earth!您的第二个位置是错误的,因为经度只能从 +180 到 -180,所以地球上不存在 -181! Secondly, as MrUpsidedown mentioned in the comment, you are calling a function within a function.其次,正如评论中提到的 MrUpsidedown,您是在函数内调用函数。 Correct the geolocation first and then your function calls, that should fix the problems you're having.首先更正地理位置,然后是您的函数调用,这应该可以解决您遇到的问题。
Before starting the code, always check/ configure your google Maps API and its subscription plan correctly (don't worry it will not cost you a cent. Just add the configurations from the cloud portal).在开始编写代码之前,请务必正确检查/配置您的 google Maps API 及其订阅计划(不要担心它不会花费您一分钱。只需从云门户添加配置即可)。 If there is an error in the above-mentioned configuration, you will not be able to load the map.如果上述配置有错误,您将无法加载地图。
Plz, refer to the following script to get the route among two points.请参阅以下脚本以获取两点之间的路线。 I have written in the simplest way for easy understanding.为了便于理解,我以最简单的方式编写。
<!DOCTYPE html>
<html>
<head>
<title>Google Map</title>
</head>
<style type="text/css">
#map{
height: 80%;
}
html , body {
height: 100%;
}
</style>
<body onload="myfunction();">
<div id="map">
</div>
<script type="text/javascript">
function myfunction(){
var map;
var start = new google.maps.LatLng(7.434876909631617,80.4424951234613);
var end = new google.maps.LatLng(7.3178281209262686,80.8735878891028);
var option ={
zoom : 10,
center : start
};
map = new google.maps.Map(document.getElementById('map'),option);
var display = new google.maps.DirectionsRenderer();
var services = new google.maps.DirectionsService();
display.setMap(map);
var request ={
origin : start,
destination:end,
travelMode: 'DRIVING'
};
services.route(request,function(result,status){
if(status =='OK'){
display.setDirections(result);
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=**Your API KEY**&libraries=places"></script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.