[英]Get and display lat long from address using Leaflet Control Search
我正在使用leaflet.js和此插件: https : //github.com/stefanocudini/leaflet-search ,需要一種從地址搜索中獲取經緯度坐標並將其放入輸入字段的方法,或者只是能夠使用它們...
答案很可能在下面的代碼中,只是不知道該怎么做...
var geocoder = new google.maps.Geocoder();
function googleGeocoding(text, callResponse)
{
geocoder.geocode({address: text}, callResponse);
}
function filterJSONCall(rawjson)
{
var json = {},
key, loc, disp = [];
for(var i in rawjson)
{
key = rawjson[i].formatted_address;
loc = L.latLng( rawjson[i].geometry.location.lat(), rawjson[i].geometry.location.lng() );
json[ key ]= loc; //key,value format
}
return json;
}
map.addControl( new L.Control.Search({
callData: googleGeocoding,
filterJSON: filterJSONCall,
wrapper: 'findbox',
markerLocation: true,
autoType: false,
autoCollapse: true,
minLength: 5,
zoom: 10,
initial: true,
collapsed: false,
tipAutoSubmit: false,
autoResize: false,
text: 'Enter an Address'
}) );
在leaflet-search.js文件中,您具有一個名為
_getLocation(this._input.value)
此函數返回您需要的信息。 您可以看到它在_handleSubmit函數內部被調用,如下所示:
var loc = this._getLocation(this._input.value);
如果您這樣做:
console.log("Latitude: " + loc.lat);
console.log("Longitude: " + loc.lng);
在leaflet-search.js中進行此調用后,您將在控制台中獲得所需的信息。
我給了您所需的信息,現在您可以根據自己的喜好使用它。 設置一個公共函數,然后在您的代碼或任何您想要的內部訪問它。
var searchbox = new L.Control.Search({
callData: googleGeocoding,
filterJSON: filterJSONCall,
wrapper: 'findbox',
markerLocation: true,
autoType: false,
autoCollapse: true,
minLength: 5,
zoom: 10,
initial: true,
collapsed: false,
tipAutoSubmit: false,
autoResize: false,
text: 'Enter an Address'
});
searchbox.on('search_locationfound', function(e) {
var locLat = e.latlng.lat;
var locLng = e.latlng.lng;
console.log(locLat+', '+locLng);
});
這對我來說很好。 我希望我可以幫助其他人。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.