UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 4.06 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import e from"../../../Graphic.js";import t from"../../../geometry/Polygon.js";import{createExtentFromGeometry as o,scaleExtent as r}from"../../../geometry/support/geometryUtils.js";import{addressToLocations as n}from"../../../rest/locator/addressToLocations.js";import{locationToAddress as s}from"../../../rest/locator/locationToAddress.js";import{suggestLocations as i}from"../../../rest/locator/suggestLocations.js";import c from"../../../rest/support/AddressToLocationsParameters.js";import a from"../../../rest/support/LocationToAddressParameters.js";import u from"../../../rest/support/SuggestLocationsParameters.js";const l="Single Line Input",d=3e5;function m(e,t){return e.location?S(e,t):h(e,t)}function f(e,t){if(t.localSearchDisabled)return null;const o=e?.scale;return"number"==typeof o&&o<=d?e?.extent?.center:null}async function p(e,t){const{source:o,spatialReference:r,view:n,suggestTerm:s,maxSuggestions:c,sourceIndex:a}=e,l=new u,{apiKey:d,url:m}=o,p=w(o,n),g=t?.signal;if(!m)return null;d&&(l.apiKey=d),o.categories&&(l.categories=o.categories),r&&(l.outSpatialReference=r);const y=f(n,o);y&&(l.location=y);if(!s.trim())return null;const{prefix:x="",suffix:v=""}=o,S=`${x}${s}${v}`;return l.text=S,p&&(l.searchExtent=p),l.maxSuggestions=c,o.countryCode&&(l.countryCode=o.countryCode),i(m,l,{signal:g}).then((e=>I(e,a)))}function g(e){return!!e&&/(?:geocode-api\.arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}function y(e){return!!e&&/(?:\/servers\/[\da-z.-]+\/rest\/services\/world\/geocodeserver).*/i.test(e)}function x(e){return!!e&&/(?:arcgis\.com\/arcgis\/rest\/services\/world\/geocodeserver).*/i.test(e)}const v="https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";async function S(e,t){const{source:o,spatialReference:r,location:n,sourceIndex:i,view:c}=e,{apiKey:u,url:l,zoomScale:d,defaultZoomScale:m}=o;if(null==l)return[];const f=c?.scale,p=t?.signal,g=new a({location:n});u&&(g.apiKey=u),r&&(g.outSpatialReference=r);try{return K([await s(l,g,{signal:p})],{sourceIndex:i,scale:f,view:c,zoomScale:d,defaultZoomScale:m})}catch(y){return[]}}function w(e,t){const{filter:r,withinViewEnabled:n}=e,s=t?.scale,i=t?.extent,c=r?.geometry;return(c?o(c,t??void 0,s):void 0)||(n&&i?i:void 0)}async function h(e,t){const{source:o,suggestResult:r,spatialReference:s,view:i,maxResults:a,sourceIndex:u}=e,d=o?.zoomScale,m=o?.defaultZoomScale,p=r.text?.trim();if(!p)return null;const g=!r.key&&o.prefix?o.prefix:"",y=!r.key&&o.suffix?o.suffix:"",x=`${g}${r.text}${y}`,v=new c,{apiKey:S,url:h}=o,j=i?.scale,I=w(o,i),$=t?.signal;if(S&&(v.apiKey=S),!h)return null;o.categories&&(v.categories=o.categories),o.locationType&&(v.locationType=o.locationType),s&&(v.outSpatialReference=s);const L=f(i,o);L&&(v.location=L),v.maxLocations=a,I&&(v.searchExtent=I),o.countryCode&&(v.countryCode=o.countryCode);const{key:R}=r,T=`${R}`;return R&&(v.magicKey=T),v.address={},v.address[o.singleLineFieldName||l]=x,o.outFields&&(v.outFields=o.outFields),n(h,v,{signal:$}).then((e=>K(e,{key:T,scale:j,sourceIndex:u,view:i,zoomScale:d,defaultZoomScale:m})))}function j(e,t){return{text:e.text,key:e.magicKey,sourceIndex:t}}function I(e,t){return e.map((e=>j(e,t)))}function $(n,s){const{key:i,scale:c,sourceIndex:a,view:u,zoomScale:l,defaultZoomScale:d}=s,{attributes:m,extent:f,location:p,address:g}=n,y=new e({geometry:p,attributes:m}),x=f||p,v=x?o(x,u??void 0,c??void 0):void 0,S=v?"number"==typeof l?r(v,u??void 0,l):"number"==typeof d&&"point"===x?.type?r(v,u??void 0,d):v:null,w=p?`${p.x},${p.y}`:"",h=g||w,j=y.clone();return null!=S&&(j.geometry=t.fromExtent(S)),{extent:S,feature:y,target:j,key:i,name:h,sourceIndex:a}}function K(e,t){return e.filter(Boolean).map((e=>$(e,t)))}export{f as getLocation,m as getResults,p as getSuggestions,x as isArcGISWorldGeocoder,g as isMeteredArcGISWorldGeocoder,y as isProxiedArcGISWorldGeocoder,v as meteredArcGISLocatorUrl};