Cyb*_*kie 3 php api jquery dictionary geonames
我一直在使用 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" );
}
});
});
Run Code Online (Sandbox Code Playgroud)
小智 5
&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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3573 次 |
| 最近记录: |