javascript – Google Map Api无效值错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Google Map Api无效值错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4021字,纯文字阅读大概需要6分钟。
内容图文
![javascript – Google Map Api无效值错误](/upload/InfoBanner/zyjiaocheng/706/0e2d5e7a415b4264ba5931c2f9f7ceb1.jpg)
我使用Google Maps API创建了以下代码,该代码应在两个指定地址之间在Google地图上生成方向线.我的代码是:
function initMap()
{
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: -34.397, lng: 150.644}
});
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
map: map
});
var geocoder = new google.maps.Geocoder();
var pointA,pointB;
geocoder.geocode({'address': document.getElementById('addressFrom').value}, function(results, status) {
var location = results[0].geometry.location;
pointA = new google.maps.LatLng(location.lat(),location.lng());
alert(location.lat() + '' + location.lng());
});
geocoder.geocode({'address': document.getElementById('addressTo').value}, function(results, status) {
var location = results[0].geometry.location;
pointB = new google.maps.LatLng(location.lat(),location.lng());
alert(location.lat() + '' + location.lng());
});
var markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "A",
map: map
});
var markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "B",
map: map
});
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB) {
directionsService.route({
origin: pointA,
destination: pointB,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
<input id="addressFrom" type="textbox" value="Sydney">
<input id="addressTo" type="textbox" value="London">
<input id="submit" type="button" value="Geocode" onclick="initMap">
<div id="map"></div>
从浏览器检查时出现以下错误:
解决方法:
地理编码器是异步的,结果直到您将呼叫发送到路线服务后才会返回.
提示是javascript控制台中的此错误:未捕获的TypeError:无法读取null的属性“l”
链接地理编码器呼叫(不确定为什么需要这些,路由服务接收地址就好了),将呼叫转移到路由服务进入第二个地理编码操作的回调函数(因此在调用路线服务时两个位置都可用) .
另一个问题是你无法从悉尼开车到伦敦.
代码段:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {
lat: -34.397,
lng: 150.644
}
});
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
map: map
});
var geocoder = new google.maps.Geocoder();
var pointA, pointB;
geocoder.geocode({
'address': document.getElementById('addressFrom').value
}, function(results, status) {
if (status != "OK") return;
var location = results[0].geometry.location;
pointA = new google.maps.LatLng(location.lat(), location.lng());
alert(location.lat() + ',' + location.lng());
var markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "A",
map: map
});
geocoder.geocode({
'address': document.getElementById('addressTo').value
}, function(results, status) {
if (status != "OK") return;
var location = results[0].geometry.location;
pointB = new google.maps.LatLng(location.lat(), location.lng());
alert(location.lat() + ',' + location.lng());
var markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "B",
map: map
});
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB);
});
});
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB) {
directionsService.route({
origin: pointA,
destination: pointB,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
html,
body,
#map {
height: 100%;
width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input id="addressFrom" type="textbox" value="Sydney" />
<input id="addressTo" type="textbox" value="London" />
<input id="submit" type="button" value="Geocode" onclick="initMap()" />
<div id="map"></div>
内容总结
以上是互联网集市为您收集整理的javascript – Google Map Api无效值错误全部内容,希望文章能够帮你解决javascript – Google Map Api无效值错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。