【发布时间】:2014-02-03 16:44:23
【问题描述】:
我有使用地理编码的 Java 脚本 Google Map Api v3。 当我在搜索框中输入例如邮政编码时,我可以从列表中选择地址。如果我会做同样的事情并按 ENTER 什么都不会发生。如何修改代码?
<input id="address" type="textbox">
Java 脚本代码:
var geocoder;
var map;
var marker;
function initialize(){
//MAP
var latlng = new google.maps.LatLng(51.469186, -0.361166);
var options = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
//GEOCODER
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
draggable: false
});
//CIRCLE
var circle = new google.maps.Circle({
map: map,
center: new google.maps.LatLng(51.469186, -0.361166),
fillColor: '#204617',
fillOpacity: 0.2,
strokeColor: '#6DE953',
strokeOpacity: 0.4,
strokeWeight: 2
});
circle.setRadius(10000);
}
$(document).ready(function() {
initialize();
$(function() {
$("#address").autocomplete({
//This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
return {
label: item.formatted_address,
value: item.formatted_address,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
}
}));
})
},
//This bit is executed upon selection of an address
select: function(event, ui) {
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
marker.setPosition(location);
map.setCenter(location);
}
});
document.getElementById("address").focus();
});
});
我已经尝试添加搜索按钮:
<input id="search" type="button" value="Search" onclick="codeAddress()">
并将函数 codeAddress() 添加到 Java Script 但我一定做错了什么,因为那没有用。
工作(不带 ENTER)jsfiddle
【问题讨论】:
-
我没有在您提供的代码中看到您的 codeAddress 函数,这将是问题所在。就像您为 on click 事件提供事件处理程序一样,您需要对 enter 事件执行相同的操作。
-
我已将
$(function() {更改为function codeAddress(){但这破坏了代码并且没有任何效果,因此我恢复了该更改。
标签: google-maps-api-3 geocoding