我一直在使用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" );
}
});
});
发布于 2011-05-04 21:39:34
正在返回lat
& lng
;请参阅此测试请求:
http://api.geonames.org/searchJSON?style=full&maxRows=12&name_startsWith=romse&username=demo
它在下面的结构中,所以你只需要访问它:
{ "geonames" : [ {
"adminCode1" : "ENG",
<SNIP>
"geonameId" : 2639189,
"lat" : 50.989057046475203,
"lng" : -1.4998912811279297
https://stackoverflow.com/questions/5889598
复制