Get and display lat long from address using Leaflet Control Search

梦想与她 提交于 2020-01-06 01:15:27

问题


I am using leaflet.js and this plugin: https://github.com/stefanocudini/leaflet-search and need a way of getting the lat and long coordinates from the address search and putting them into an input field, or just being able to use them...

The answer may well be in the code below, just can't figure out how to do it...

                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'
                    }) );

回答1:


Inside the leaflet-search.js file you have a function called

_getLocation(this._input.value)

This function returns the information you need. You can see it's called inside the _handleSubmit function like this:

var loc = this._getLocation(this._input.value);

If you do:

console.log("Latitude: " + loc.lat);
console.log("Longitude: " + loc.lng);

bellow this call in leaflet-search.js you will get the info you need in the console.

I gave you the info you needed, now it's up to you to use it how you like. Make a public function then access it inside your code or whatever you want.




回答2:


 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);
            });

This works fine with me. I hope I might help other people with this. :)



来源:https://stackoverflow.com/questions/31280978/get-and-display-lat-long-from-address-using-leaflet-control-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!