从谷歌地图中的路线中获取起点和终点地理代码
Get start and end geocode from the directions route in google maps
我正在根据需要定制这个google maps direction
示例
我想知道路线上起点和终点的纬度/Lng。如何获取这些坐标?
我一直在调试响应,但找不到这些坐标。
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -24.345, lng: 134.46} // Australia.
});
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
panel: document.getElementById('right-panel')
});
directionsDisplay.addListener('directions_changed', function() {
computeTotalDistance(directionsDisplay.getDirections());
});
displayRoute('Perth, WA', 'Sydney, NSW', directionsService,
directionsDisplay);
}
function displayRoute(origin, destination, service, display) {
service.route({
origin: origin,
destination: destination,
waypoints: [{location: 'Adelaide, SA'}, {location: 'Broken Hill, NSW'}],
travelMode: 'DRIVING',
avoidTolls: true
}, function(response, status) {
if (status === 'OK') {
display.setDirections(response);
} else {
alert('Could not display directions due to: ' + status);
}
});
}
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000;
document.getElementById('total').innerHTML = total + ' km';
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
float: left;
width: 63%;
height: 100%;
}
#right-panel {
float: right;
width: 34%;
height: 100%;
}
.panel {
height: 100%;
overflow: auto;
}
<div id="map"></div>
<div id="right-panel">
<p>Total Distance: <span id="total"></span></p>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
结果中的DirectionsLeg对象中提供了每条腿的开始和结束坐标。
DirectionsLeg对象的文档:
google.maps.DirectionsLeg对象规范
由DirectionsResult中的一组步骤组成的单个分支。分支中的某些字段可能不会针对所有请求返回。注意,尽管该结果是"0";类似JSON;它不是严格意义上的JSON,因为它直接和间接地包括LatLng对象。
end_location|类型:LatLng
DirectionsService通过在起点和终点使用最近的交通选项(通常是道路)来计算地点之间的方向。end_location表示实际的地理编码目的地,例如,如果道路不在该路段的目的地附近,则其可能不同于上一步的end_location。
start_location|类型:LatLng
DirectionsService通过在起点和终点使用最近的交通选项(通常是道路)来计算地点之间的方向。start_location表示实际的地理编码原点,例如,如果道路不在该路段的原点附近,则其可能不同于第一步的start_location。
解析失控响应的示例代码:
for (var i=0;i<response.routes.length;i++){
for (var j=0;j<response.routes[i].legs.length; j++) {
document.getElementById('info').innerHTML += "["+j+"] start coord="+response.routes[i].legs[j].start_location.toUrlValue(6)+"<br>";
document.getElementById('info').innerHTML += "["+j+"] end coord="+response.routes[i].legs[j].end_location.toUrlValue(6)+"<br>";
}
}
概念验证小提琴
代码片段:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {
lat: -24.345,
lng: 134.46
} // Australia.
});
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
panel: document.getElementById('right-panel')
});
directionsDisplay.addListener('directions_changed', function() {
computeTotalDistance(directionsDisplay.getDirections());
});
displayRoute('Perth, WA', 'Sydney, NSW', directionsService,
directionsDisplay);
}
function displayRoute(origin, destination, service, display) {
service.route({
origin: origin,
destination: destination,
waypoints: [{
location: 'Adelaide, SA'
}, {
location: 'Broken Hill, NSW'
}],
travelMode: 'DRIVING',
avoidTolls: true
}, function(response, status) {
if (status === 'OK') {
display.setDirections(response);
for (var i = 0; i < response.routes.length; i++) {
for (var j = 0; j < response.routes[i].legs.length; j++) {
document.getElementById('info').innerHTML += "[" + j + "] start coord=" + response.routes[i].legs[j].start_location.toUrlValue(6) + "<br>";
document.getElementById('info').innerHTML += "[" + j + "] end coord=" + response.routes[i].legs[j].end_location.toUrlValue(6) + "<br>";
}
}
} else {
alert('Could not display directions due to: ' + status);
}
});
}
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000;
document.getElementById('total').innerHTML = total + ' km';
}
#right-panel {
font-family: 'Roboto', 'sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select,
#right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
float: left;
width: 63%;
height: 100%;
}
#right-panel {
float: right;
width: 34%;
height: 100%;
}
.panel {
height: 100%;
overflow: auto;
}
<div id="info"></div>
<div id="map"></div>
<div id="right-panel">
<p>Total Distance: <span id="total"></span>
</p>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- 代码不会验证
- 如何从rails中的代码中删除新行( )
- Angularjs代码未在匿名函数中运行
- 如何做到这一点,使代码在不传递条件后执行函数
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 面向对象的Javascript代码在IE7中不起作用
- 将 jQuery 代码添加到 Index.aspx 页面
- 为什么忽略了eval()代码中的语法错误
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 从谷歌地图中的路线中获取起点和终点地理代码