pos-gis/public/js/esri-leaflet-geocoder.js

4 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

2024-10-07 06:13:42 +00:00
/*! esri-leaflet-geocoder - v0.0.1-beta.5 - 2014-06-20
* Copyright (c) 2014 Environmental Systems Research Institute, Inc.
* Apache 2.0 License */
!function(a){a.esri.Services.Geocoding=a.esri.Services.Service.extend({statics:{WorldGeocodingService:"https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/"},includes:a.Mixin.Events,initialize:function(b,c){b="string"==typeof b?b:a.esri.Services.Geocoding.WorldGeocodingService,c="object"==typeof b?b:c,this.url=a.esri.Util.cleanUrl(b),a.Util.setOptions(this,c),a.esri.Services.Service.prototype.initialize.call(this,b,c)},geocode:function(b,c,d,e){var f={outFields:"Subregion, Region, PlaceName, Match_addr, Country, Addr_type, City, Place_addr"},g=a.extend(f,c);g.text=b,this.get("find",g,function(a,c){if(a)d.call(e,a);else{for(var f=[],g=c.locations.length-1;g>=0;g--){var h=c.locations[g];f.push(this._processResult(b,h))}d.call(e,a,f,c)}},this)},reverse:function(b,c,d,e){var f=c||{};f.location=[b.lng,b.lat].join(","),this.get("reverseGeocode",f,function(b,c){if(b)d.call(e,b);else{var f=c.address,g={latlng:new a.LatLng(c.location.y,c.location.x),address:f.Address,neighborhood:f.Neighborhood,city:f.City,subregion:f.Subregion,region:f.Region,postal:f.Postal,postalExt:f.PostalExt,countryCode:f.CountryCode};d.call(e,b,g,c)}},this)},suggest:function(a,b,c,d){var e=b||{};e.text=a,this.get("suggest",e,c,d)},_processResult:function(b,c){var d=c.feature.attributes,e=a.esri.Util.extentToBounds(c.extent);return{text:b,bounds:e,latlng:new a.LatLng(c.feature.geometry.y,c.feature.geometry.x),name:d.PlaceName,match:d.Addr_type,country:d.Country,region:d.Region,subregion:d.Subregion,city:d.City,address:d.Place_addr?d.Place_addr:d.Match_addr}}}),a.esri.Services.geocoding=function(b){return new a.esri.Services.Geocoding(b)},a.esri.Controls.Geosearch=a.Control.extend({includes:a.Mixin.Events,options:{position:"topleft",zoomToResult:!0,useMapBounds:12,collapseAfterResult:!0,expanded:!1,maxResults:25,forStorage:!1,allowMultipleResults:!0},initialize:function(b){a.Util.setOptions(this,b),this._service=new a.esri.Services.Geocoding(b),this._service.on("authenticationrequired requeststart requestend requesterror requestsuccess",function(b){b=a.extend({target:this},b),this.fire(b.type,b)},this)},_geocode:function(b,c){var d={};if(c)d.magicKey=c;else if(d.maxLocations=this.options.maxResults,this.options.useMapBounds===!0||this.options.useMapBounds<=this._map.getZoom()){var e=this._map.getBounds(),f=e.getCenter(),g=e.getNorthWest();d.bbox=e.toBBoxString(),d.location=f.lng+","+f.lat,d.distance=Math.min(Math.max(f.distanceTo(g),2e3),5e4)}this.options.forStorage&&(d.forStorage=!0),a.DomUtil.addClass(this._input,"geocoder-control-loading"),this.fire("loading"),this._service.geocode(b,d,function(c,d){if(d&&d.length){var e,f=new a.LatLngBounds;for(e=d.length-1;e>=0;e--)f.extend(d[e].bounds);this.fire("results",{results:d,bounds:f,latlng:f.getCenter()}),this.options.zoomToResult&&this._map.fitBounds(f)}else this.fire("results",{results:[],bounds:null,latlng:null,text:b});a.DomUtil.removeClass(this._input,"geocoder-control-loading"),this.fire("load"),this.clear(),this._input.blur()},this)},_suggest:function(b){a.DomUtil.addClass(this._input,"geocoder-control-loading");var c={};if(this.options.useMapBounds===!0||this.options.useMapBounds<=this._map.getZoom()){var d=this._map.getBounds(),e=d.getCenter(),f=d.getNorthWest();c.location=e.lng+","+e.lat,c.distance=Math.min(Math.max(e.distanceTo(f),2e3),5e4)}this._service.suggest(b,c,function(b,c){if(this._input.value){if(this._suggestions.innerHTML="",this._suggestions.style.display="none",c&&c.suggestions){this._suggestions.style.display="block";for(var d=0;d<c.suggestions.length;d++){var e=a.DomUtil.create("li","geocoder-control-suggestion",this._suggestions);e.innerHTML=c.suggestions[d].text,e["data-magic-key"]=c.suggestions[d].magicKey}}a.DomUtil.removeClass(this._input,"geocoder-control-loading")}},this)},clear:function(){this._suggestions.innerHTML="",this._suggestions.style.display="none",this._input.value="",this.options.collapseAfterResult&&a.DomUtil.removeClass(this._container,"geocoder-control-expanded")},onAdd:function(b){return this._map=b,b.attributionControl&&b.attributionControl.addA