Select to view content in your preferred language

Map without API key

6514
12
10-11-2021 01:07 PM
ScottAndersonGQ
New Contributor III

I wrote some code that creates a map and allows users to click on the map. The click takes the lat,lng (of click) and queries a government's GIS database and returns the parcel number. They can also use the default search widget and search by address, which ultimately does the same thing. 

I also added a polygon layer that outlines parcels and another polygon layer that outlines houses. 

I am NOT USING AN API KEY. I am NOT using esriConfig. 

The code works and seems fairly easy to disseminate. I just add:

<script src="https://js.arcgis.com/4.20/"></script>

And the code just works. Basemap layers and Geocoding are a part of the Pricing, although nothing is stopping my code from running. 

The Questions is: Do I still need an API key? If so, does the search widget use a Geocoding credit? Does the adding a layer or loading the basemap use a Basemap credit? 
If not, why not? (But also not complaining)

Extra Info:
To Query I use -> {featurelayerlink}/query?
geometry={lat} {lng}&...etc
To Add a Layer I use -> map.add(
new FeatureLayer({          url: featureLayerURL, ...etc    }))
To Make the map -> map = new Map({        basemap: "streets",      });
Tags (4)
0 Kudos
12 Replies
MichaelKatz-NOAAAffiliate
New Contributor

@BjornSvensson I am part of an organization (NOAA) that has an Esri enterprise license. I want to use vector basemaps without an API key as those above are doing, but I need to use them through OpenLayers, not through the Ersi JavaScript API. I see examples like https://codepen.io/lndr27/pen/yLzyYNw (reference above) that use the Esri API and just have to specify basemap: "satellite" as part of making their map object. But in the OpenLayers examples such as https://developers.arcgis.com/openlayers/layers/add-a-vector-tile-layer/, an API key is used, and trying to leave it off the URL results in a 401 permissions error for me. Can you point me to an example of using a styled vector basemap under OpenLayers without an API key?

I was able to get as far as the code below. It adds the layer, but the layer is just in a blue outline, which I take to be the layer without any styling applied. Is there a way for me to apply styles to this layer (without an API key)? Or is this the wrong approach?

 

    const streetsSource = new ol.source.VectorTile(
        {
            format: new ol.format.MVT(),
            url: `https://basemaps.arcgis.com/arcgis/rest/services/World_Basemap_v2/VectorTileServer/tile/{z}/{y}/{x}.pbf`
        } );
   
    const streetsLayer = new ol.layer.VectorTile(
        {
            source: streetsSource
        } );
   
    map.addLayer( streetsLayer );

 

0 Kudos
Gowin
by Esri Contributor
Esri Contributor

Hi there,

You have the correct URL to access the enhanced endpoint of the basemap styles service, which does allow you to access basemaps without an API key. 

The standard practice for styling ArcGIS vector basemaps in OpenLayers is to request the layer info as Mapbox style JSON and use ol-mapbox-style to load the basemap. The OpenLayers "Display a map" tutorial is a good example of this pattern. However, the enhanced endpoint that you are using does not support mapbox style JSON, which is a requirement to style basemaps in OpenLayers.

Unfortunately, Mapbox style JSON is only supported by the new basemap styles endpoint (docs here), which does require an API key / other access token. I'm not sure if there is a workaround for your specific use case. I will look into this more.

My suggestion for now would be to implement an Image tile basemap instead (doesn't require authentication). Alternatively, if API keys are the limiting factor, I would recommend that you implement an alternative method of authentication such as user authentication or app credential authentication.

Hope this helps.

0 Kudos
BjornSvensson
Esri Regular Contributor

@MichaelKatz-NOAAAffiliate , hopefully you have already gotten an answer to your question about OpenLayers.  If not, it's better to ask in the Open Source Mapping Libraries community -- https://community.esri.com/t5/open-source-mapping-libraries/ct-p/open-source-mapping-libraries 

0 Kudos