我有一个页面,用户必须在 map 上选择一个地点。他仍然可以寻找它。
在移动标记时,我希望他知道“他在哪里”,并且该值也存储在隐藏输入中,就像纬度和经度一样,以便稍后可以用于将值插入数据库中。
一切工作正常,除了在我插入地理编码功能后(可能是巧合),搜索框不起作用。它显示结果,但是当您单击时什么也没有发生。我尝试删除地理编码,但仍然不起作用。
我也遇到了 OVER_QUERY_LIMIT。
这是我的代码:
function initialize() {
var geocoder = new google.maps.Geocoder();
var myLatlng = new google.maps.LatLng(-1.456688, -48.477586);
var mapOptions = {
zoom: 14,
minZoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
draggable: true,
title: 'Hello World!'
});
google.maps.event.addListener(marker, 'position_changed', function() {
var lat = this.getPosition().lat();
var lng = this.getPosition().lng();
var latlng = new google.maps.LatLng(lat, lng);
document.getElementById('latitude').value = lat;
document.getElementById('longitude').value = lng;
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.getElementById('enderec').value = results[0].formatted_address;
$('.onde-esta').html(results[0].formatted_address);
} else {
alert("Sem resultados");
}
} else {
alert("Geocoder falhou: " + status);
}
});
});
// Create an Autocomplete and link it to the UI element.
var input = /** @type {HTMLInputElement} */ (
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var searchBox = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */
(input), {
types: ['geocode']
});
// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'place_changed', function() {
var place = this.getPlace();
document.getElementById('enderecoo').value = place.formatted_address;
//when place has been found
if (place.geometry) {
marker.setOptions({
title: place.name,
position: place.geometry.location
});
if (place.geometry.viewport) {
marker.getMap().fitBounds(place.geometry.viewport);
} else {
marker.getMap().setCenter(place.geometry.location);
}
}
else {
marker.setOptions({
title: null
});
alert('Lugar não encontrado');
}
});
// Bias the SearchBox results towards places that are within the bounds of the
// current map's viewport.
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
是因为每次用户移动标记时它都会进行地理编码吗?无论如何,只有当用户删除标记时,我才能对其进行地理编码吗? 提前致谢, 马特乌斯
最佳答案
是的。我认为您对地理编码 api 的调用太多或太快。仅当用户放下标记时才尝试使用它。
google.maps.event.addListener(Marker, "dragend", function(event) {
/*
User stopped dragging.
*/
});
关于javascript - 地理编码限制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530021/