我一直在使用Google Geocoding,直到发现Geocoding API只能与Google地图一起使用。这很不方便,因为我不需要显示地图。所以我转向了非常棒的Geonames!
我获取地址的纬度和经度,并将它们添加到我的数据库中。
以下是用于文本输入的Jquery代码,用户添加他们的城市/镇,会出现自动完成,假定他们正在打字的城市。它工作得很好,但我还想在2个隐藏的表单字段中添加他们城市的纬度和经度,以便发送到数据库。
如何从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" );
}
});
});
item.lat
,它检索到了纬度! :) 你知道在select: function( event, ui ) {
中有没有一种检索它的方法吗? - CyberJunkie