Google maps 谷歌地理编码器加载问题
在查看了GoogleAPI文档之后,我仍然感到困惑。我想使用谷歌地理编码API,但控制台抛出了错误 未定义地理编码器 是什么导致它在使用定义后抛出此错误Google maps 谷歌地理编码器加载问题,google-maps,meteor,google-maps-api-3,Google Maps,Meteor,Google Maps Api 3,在查看了GoogleAPI文档之后,我仍然感到困惑。我想使用谷歌地理编码API,但控制台抛出了错误 未定义地理编码器 是什么导致它在使用定义后抛出此错误 var geocoder = new google.maps.Geocoder(); 这是我的地图代码 GoogleMaps.ready('locationRadiusMap', function(map) { var markers = []; var input = $("#pac-input")[0];
var geocoder = new google.maps.Geocoder();
这是我的地图代码
GoogleMaps.ready('locationRadiusMap', function(map) {
var markers = [];
var input = $("#pac-input")[0];
var searchBox = new google.maps.places.SearchBox(input);
var instance = GoogleMaps.maps.locationRadiusMap.instance;
var geocoder = new google.maps.Geocoder();
var autocomplete = new google.maps.places.Autocomplete(input);
// instance.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
google.maps.event.addListener(searchBox, 'places_changed', function(m) {
function geocodeLatLng(geocoder, map, information) {
var geocoder = new google.maps.Geocoder();
var userlat = Session.get('userlat');
var userlng = Session.get('userlng');
var latLng = userlat.toString() + ", " + userlng.toString();
var latLngStr = latLng.split(",", 2);
var latLngLocate = {
lat: parseFloat(latLngStr[0]),
lng: parseFloat(latLngStr[1])
};
geocoder.geocode({
'location': latLngLocate
}, function(results, status) {
if (status === Geocoder.status.OK) {
if (results[1]) {
console.log(results[1].formatted_address);
}
}
else {
window.alert('No results found');
}
});
}
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
markers.forEach(function(marker) {
marker.setMap(null);
// areaCircle.setMap(null);
});
markers = [];
places.forEach(function(place) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var areaCircle = new google.maps.Circle({
map: map.instance,
center: place.geometry.location,
zoom: 7,
radius: 8093.4,
strokeColor: "#f8504b",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#f8504b",
fillOpacity: 0.4
});
var marker = new google.maps.Marker({
map: map.instance,
icon: image,
title: place.name,
draggable: true,
position: place.geometry.location,
animation: google.maps.Animation.DROP
});
// Create a marker for each place.
markers.push(marker);
console.log(marker);
var userlat = marker.getPosition().lat();
var userlng = marker.getPosition().lng();
var locate = marker.getPosition();
console.log(locate);
Session.set("userlat", userlat);
Session.set("userlng", userlng);
console.log(userlat, userlng);
var latLng = userlat.toString() + ", " + userlng.toString();
geocodeLatLng(geocoder, map);
var mape = map.instance;
mape.setCenter(marker.getPosition());
这将使用应用程序密钥加载地图
GoogleMaps.load({
v: '3',
key: 'KEYHERE',
libraries: 'geometry,places'
});
在这条线上爆炸了:
if (status === Geocoder.status.OK) {
试试这个:
if (status === google.maps.GeocoderStatus.OK) {
google.maps.GeocoderStatus应该存在,但Geocoder不存在。即使只是一行代码,我是否应该始终使用JS代码框?只是好奇谢谢你的快速回复。我尝试了您提供的两个选项,它给出了另一个错误Uncaught TypeError:无法读取未定义的属性“OK”。请尝试在定义geocoder:console.log(google.maps.GeocoderStatus.OK)之后添加此行。我正在测试google的版本能够找到这个ok,并且它在官方文档中,所以如果它在您的回调中不起作用,那么我就被难住了……它现在可以工作了,谢谢,我添加了console.log,现在它在注销该值时神奇地工作了。