在审核Google API文档后,我仍然感到困惑。我想使用Google地理编码API,但控制台会抛出错误
未定义Geocoder
在用
定义之后导致它抛出此错误的原因是什么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());
使用app键
加载地图GoogleMaps.load({
v: '3',
key: 'KEYHERE',
libraries: 'geometry,places'
});
答案 0 :(得分:1)
它在这条线上爆炸:
if (status === Geocoder.status.OK) {
试试这个:
if (status === google.maps.GeocoderStatus.OK) {
google.maps.GeocoderStatus应该存在,但Geocoder不存在。