无法获取gmap地理编码数据
Cannot get gmaps geocoding data
我在表单中有一个隐藏字段:
<form>
<input id="distance" name="km" type="hidden">
</form>
我只想用用户选择的两个地址之间的距离填充这个字段。然后,我只需将其与表单一起提交,并对其进行服务器操作。这里我调用calculateDistance
函数:
$('#my_form_id').on("submit", function (e) {
calculateDistance();
// other stuff
return true;
});
这是我想要得到的距离:
function calculateDistance() {
var startPoint = $('#main_from_route_input').val();
var endPoint = $('#main_to_route_input').val();
var geocoderStart = new google.maps.Geocoder();
var geocoderEnd = new google.maps.Geocoder();
var coordsStart, coordsEnd;
geocoderStart.geocode({'address': startPoint}, function (response, status) {
if(status != google.maps.GeocoderStatus.OK){
alert('Geocode of first address failed: ' + status);
}
coordsStart = response[0].geometry.location;
geocoderEnd.geocode({'address': endPoint}, function (response, status) {
if(status != google.maps.GeocoderStatus.OK){
alert('Geocode of second address failed: ' + status);
}
coordsEnd = response[0].geometry.location;
var distance = (google.maps.geometry.spherical.computeDistanceBetween(coordsStart, coordsEnd) / 1000).toFixed(2);
$('#distance').val(distance);
});
});
}
我得到了上解。但我还没讲到回调函数。当我调试并到达回调时,它只是被跳过。代码似乎是正确的(与其他答案相同)。问题在哪里?
例如,我收到的地址值为'Copenhagen, Denmark'
和'Berlin, Germany'
。我试着让它在谷歌代码片段的建议,如下所示:'Copenhagen,+Denmark'
(空白空间被替换为+
),但仍然不起作用。
问题:
- 你不能从异步回调函数返回任何东西,你需要使用回调函数内部的数据。
- 在地理编码结果返回之前提交表单(在onsubmit函数中返回false,然后在所有回调处理完成后提交表单)。
- 您正在计算直线距离(
computeDistanceBetween
)并将其与驾驶距离进行比较。
概念验证
代码片段:
var geocoder;
var map;
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
calculateDistance();
}
function calculateDistance() {
var bounds = new google.maps.LatLngBounds();
var path = [];
var startPoint = $('#main_from_route_input').val();
var endPoint = $('#main_to_route_input').val();
var geocoderStart = new google.maps.Geocoder();
var geocoderEnd = new google.maps.Geocoder();
var coordsStart, coordsEnd;
geocoderStart.geocode({
'address': startPoint
}, function(response, status) {
if (status != google.maps.GeocoderStatus.OK) {
alert('Geocode of first address failed: ' + status);
}
coordsStart = response[0].geometry.location;
bounds.extend(coordsStart);
var startMark = new google.maps.Marker({
position: coordsStart,
map: map,
title: "start"
});
path.push(coordsStart);
geocoderEnd.geocode({
'address': endPoint
}, function(response, status) {
if (status != google.maps.GeocoderStatus.OK) {
alert('Geocode of second address failed: ' + status);
}
coordsEnd = response[0].geometry.location;
bounds.extend(coordsEnd);
var endMark = new google.maps.Marker({
position: coordsEnd,
map: map,
title: "end"
});
path.push(coordsEnd);
var polyline = new google.maps.Polyline({
path: path,
map: map
});
var distance = (google.maps.geometry.spherical.computeDistanceBetween(coordsStart, coordsEnd) / 1000).toFixed(2);
$('#distance').val(distance);
map.fitBounds(bounds);
});
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<form>
<input id="distance" name="km">
<input id="main_from_route_input" value="Copenhagen, Denmark" />
<input id="main_to_route_input" value="Berlin, Germany" />
</form>
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
相关文章:
- Json在完整日历中对数据进行了编码
- 如何将JSON编码的PHP数组发送到同一页面上的数据表
- Json在js中对数据循环进行编码
- 如何将这些数据编码为JSON中的父/子结构
- 无法读取js中的php JSON编码数据
- 从PhoneGap FileEntry中提取base64编码的数据
- 将谷歌地理编码(从url)数据加载到javascript变量中
- 在我的代码中管理大量硬编码数据的最佳方法
- JSON编码数据未解析到PhP脚本中的JqPlot图表
- Rally App SDK 2.0:Rallymultiobjectpicker的硬编码数据值
- PHP 通过 url 编码数据传递字符串
- 如何使用 sql 行 id 使用使用 JSON 编码数据定义元素
- 无法获取gmap地理编码数据
- 如何读取一个ArrayBuffer与二进制数据在“4字节”单/浮点/IEEE 754编码数据
- 如何在javascript中获得编码数据
- 如何使用UTF-8编码数据执行融合表请求
- 如何通过相同的流发送不同的编码数据
- 超大.js如何在设置脚本中调用 JSON 编码数据
- PhoneGap - 发送 JSON 编码数据错误
- Javascript base64在textarea中编码数据并替换