Get map coordinates
Overview
You will learn: how to display the latitude and longitude, scale, and zoom level of the map in a custom widget.
The out-of-the-box Map widget in ArcGIS Experience Builder contains an instance of a View from the ArcGIS API for JavaScript. The View
provides a way to interact with the map and to retrieve information about the map location. Using properties and event handlers on the View
you can find the current spatial reference information, latitude and longitude, scale, and zoom level for the map or any screen point location. Once you have this information you can display it in a custom widget, use it to find other locations on the earth, or use it to set the initial extent of your Map when your Experience starts.
In this tutorial, you will access the map widget and display the latitude, longitude, scale, and zoom level of the map in your custom widget.
Prerequisites
- Be sure to download, install, and configure ArcGIS Experience Builder.
- Complete the create a starter widget tutorial. It forms the base of this tutorial.
Steps
Get the starter widget
Download the Starter Widget template here.
In the ArcGIS Experience Builder folder, extract the zip into the following path: /client/your-extensions/widgets.
Change the widget name
In the terminal where the
npm start
is running in the client folder, stop the script by pressingctrl + c
.In your file browser, go to the folder where ArcGIS Experience Builder was extracted.
In the ArcGIS Experience Builder folder, expand the following path: /client/your-extensions/widgets.
In the widgets folder, rename the starter-widget folder to
get-map-coordinates
.In the newly-renamed get-map-coordinates folder, open the manifest.json file in the code editor.
In the code editor, modify the
name
property toget-map-coordinates
. ::: labs-info It is important that thename
property of themanifest.json
matches the name of the folder for your widget. :::After the
version
property in manifest.json, add ajimu-arcgis
dependency. Declaring this allows the usage of ArcGIS API for JavaScript modules within the widget.
Implement the settings panel
A settings panel can be implemented to allow Experience authors to customize the widget. The settings panel appears in the right-hand sidebar when a widget is selected in ArcGIS Experience Builder. To create the panel, implement the BaseWidgetSetting
class.
In the widget root folder, create a
config.json
file that contains an empty object.
- In the src folder, create another folder called setting.
- In the setting folder, create a
setting.tsx
file. - Open the setting/setting.tsx file and include the following import statements.
- Add code to implement
BaseWidgetSetting
.
- In the terminal, stop (
ctrl + c
) (if applicable) and start thenpm start
script in the client folder.
Enable selecting a map view data source
In ArcGIS Experience Builder, there can be more than one Map Widget on the page at a time. Because of this, a custom widget must have a section of its Settings Panel that allows the author to choose which map widget to use.
In the setting/setting.tsx file, include the
JimuMapViewSelector
module from thejimu
library.Before the
render
function, define theonMapWidgetSelected
function.In the
render
function, in thereturn()
statement, add a tag representing theJimuMapViewSelector
.
Access the map
In the previous step, the settings panel was enhanced to allow the Map widget to be selected. The map object can be accessed using the JimuMapViewComponent
.
In the widget.tsx file, add the
JimuMapViewComponent
and theJimuMapView
type from thejimu
library.Before the
render
function, set up the default state.Add a function to update the state every time the JimuMapView data source changes.
In the
render
function, add theJimuMapViewComponent
to the JSX markup.
Track the latitude and longitude
To display the
latitude
andlongitude
property of the mouse, the mouse pointer state must be tracked. In the widget.tsx file, addlatitude
andlongitude
properties to the state object.At the top of the file, import the
Point
class.In the
activeViewChangeHandler
function, just below thesetState
command, get the latitude and longitude and update the state every time thepointer-move
event is triggered. ::: labs-info Create a Point object using the incoming mouse x and y position and then convert the coordinates to map coordinates using theview.toMap()
function. Set the state of the latitude and longitude to display the coordinate values. :::
Display the latitude and longitude coordinates
With that latitude and longitude in the component's state, you can easily display the values in the render
function.
- In the
render
function, in thereturn()
statement (right after theJimuMapViewComponent
placed earlier), add JSX to display the latitude and longitude.
Test the widget
Once the code changes have been made, you can test your widget by running ArcGIS Experience Builder and viewing your Experience.
In a web browser, go to ArcGIS Experience Builder. e.g. https://localhost:3001
::: labs-info If ArcGIS Experience Builder did not open a tab for you, browse to https://localhost:3001. If you get a "Invalid SSL Certificate" issue, click "Proceed Anyway". :::
In ArcGIS Experience Builder, click Create New to create a new experience page.
Click the Create button on the Blank scrolling template.
The Insert widget panel is opened for you. From there, drag a Map widget and your new Get Map Coordinates widget onto the experience. ::: labs-tooltip The widget may show an invalid icon right now. That's ok - you have not created one yet!
In the widget settings panel, choose Map 1 from the map selection dropdown.
In the ArcGIS Experience Builder toolbar, click Save then Preview and the Experience will open in a new browser tab with your custom widget and a map.
Congratulations, you're done!
In the ArcGIS Experience Builder preview, hover over the map to see the latitude and longitude values dynamically change in the custom widget. Compare your widget with our completed widget.