[英]Google Maps API: Geocoder: unknown property function:
我目前正在尝试使用Google Geocoder API将地址转换为纬度/经度坐标,但是,在其开发人员文档( https://developers.google.com/maps/documentation/javascript/geocoding )上使用示例时,我遇到错误“ InvalidValueError:未知属性函数”
在我的HTML文件中,我调用了Google Map API和一个Map div,应在其中渲染我的地图。 (当然,我也有其他内容,但是仅出于直截了当的目的,我只会发布相关代码)
<script src="https://maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap" async defer></script>
<div id="map"></div>
在我的JS文件中:
window.initMap = function(){
var fromLocation = new google.maps.LatLng(37.09024, -95.712891),
destLocation = new google.maps.LatLng(37.09024, -95.712891),
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.09024 , lng: -95.712891}, // center of USA
zoom: 4 // continet level
}),
directionService = new google.maps.DirectionsService(),
directionRender = new google.maps.DirectionsRenderer({
map: map
}),
markerA = new google.maps.Marker({
position: fromLocation,
title: "Point A",
label: "From",
map:map
}),
markerB = new google.maps.Marker({
position: destLocation,
title: "Point B",
label:"Destination",
map:map
});
getLatLng("Los Angeles");
} // end of initMap
function getLatLng(address){
geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address, function(results,status){
if(status === 'OK'){
console.log(results[0].geometry.location);
}
else{
console.log("Geocoding couldn't convert string address to lat/long points");
}
}
});// end of geocoder method.
}
关于如何修复未知属性函数错误的任何想法? 谢谢!
您在getLatLng
函数中有一个错字:
geocoder.geocode({
address: address,
address: address
后必须有一个“}” address: address
function getLatLng(address) {
geocoder = new google.maps.Geocoder();
geocoder.geocode({
address: address},
function(results, status) {
if (status === 'OK') {
console.log(results[0].geometry.location);
} else {
console.log("Geocoding couldn't convert string address to lat/long points");
}
}); // end of geocoder method.
}
window.initMap = function() { var fromLocation = new google.maps.LatLng(37.09024, -95.712891), destLocation = new google.maps.LatLng(37.09024, -95.712891), map = new google.maps.Map(document.getElementById('map'), { center: { lat: 37.09024, lng: -95.712891 }, // center of USA zoom: 4 // continet level }), directionService = new google.maps.DirectionsService(), directionRender = new google.maps.DirectionsRenderer({ map: map }), markerA = new google.maps.Marker({ position: fromLocation, title: "Point A", label: "From", map: map }), markerB = new google.maps.Marker({ position: destLocation, title: "Point B", label: "Destination", map: map }); getLatLng("Los Angeles"); } // end of initMap function getLatLng(address) { geocoder = new google.maps.Geocoder(); geocoder.geocode({ address: address }, function(results, status) { if (status === 'OK') { console.log(results[0].geometry.location); } else { console.log("Geocoding couldn't convert string address to lat/long points"); } }); // end of geocoder method. }
html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script> <div id="map"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.