【发布时间】:2011-08-18 21:02:38
【问题描述】:
我一直在使用 Google Geocoding 进行编码,直到我发现 Geocoding API 只能与 Google 地图一起使用。这很不方便,因为我不需要显示地图。所以我改用 Geonames,这太棒了!
我得到一个地址的纬度和经度并将它们添加到我的数据库中。
下面的代码是用于文本输入的 Jquery,用户可以在其中添加他们的城市/城镇,然后会出现一个自动完成功能,假设他们正在输入的城市。它很好用,但我还想在 2 个隐藏的表单字段中添加他们所在城市的纬度和经度,以便发送到数据库。
如何从 Geonames 中检索这些坐标?
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
【问题讨论】:
标签: php jquery api dictionary geonames