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.
Be sure to download, install, and configure ArcGIS Experience Builder Developer Edition.
Download the Starter Widget template here.
In the ArcGIS Experience Builder folder, extract the zip into the following path: /client/your-extensions/widgets.
In the terminal where the npm start
is running in the client folder, stop the script by pressing ctrl + 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 to get-map-coordinates
.
{
// *** UPDATE ***
// "name": "starter-widget",
"name": "get-map-coordinates",
"type": "widget",
"version": "1.2.0",
After the version
property in manifest.json, add a jimu-arcgis
dependency. Declaring this allows the usage of ArcGIS API for JavaScript modules within the widget.
{
"name": "get-map-coordinates",
"type": "widget",
"version": "1.2.0",
// *** ADD ***
"dependency": "jimu-arcgis",
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.
{}
setting.tsx
file./** @jsx jsx */
import { React, jsx } from "jimu-core";
import { BaseWidgetSetting, AllWidgetSettingProps } from "jimu-for-builder";
BaseWidgetSetting
.export default class Setting extends BaseWidgetSetting<AllWidgetSettingProps<any>, any> {
render() {
return <div className="widget-setting-demo">This is your starter widget setting area!</div>;
}
}
ctrl + c
) (if applicable) and start the npm start
script in the client folder.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 the jimu
library.
import { JimuMapViewSelector } from "jimu-ui/advanced/setting-components";
Before the render
function, define the onMapWidgetSelected
function.
// *** ADD ***
onMapWidgetSelected = (useMapWidgetIds: string[]) => {
this.props.onSettingChange({
id: this.props.id,
useMapWidgetIds: useMapWidgetIds
});
};
render() {
return <div className="widget-setting-demo">This is your starter widget setting area!</div>;
}
In the render
function, in the return()
statement, add a tag representing the JimuMapViewSelector
.
render() {
return <div className="widget-setting-demo">
<JimuMapViewSelector
useMapWidgetIds={this.props.useMapWidgetIds}
onSelect={this.onMapWidgetSelected}
/>
</div>;
}
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 the JimuMapView
type from the jimu
library.
import { JimuMapViewComponent, JimuMapView } from "jimu-arcgis";
Before the render
function, set up the default state.
export default class Widget extends BaseWidget<AllWidgetProps<any>, any> {
// *** ADD ***//
state = {
jimuMapView: null
};
render() {
Add a function to update the state every time the JimuMapView data source changes.
state = {
jimuMapView: null
};
// *** ADD ***//
activeViewChangeHandler = (jmv: JimuMapView) => {
if (jmv) {
this.setState({
jimuMapView: jmv
});
}
};
In the render
function, add the JimuMapViewComponent
to the JSX markup.
render() {
return (
<div className="widget-starter jimu-widget">
{/* *** ADD *** */}
{this.props.hasOwnProperty("useMapWidgetIds") &&
this.props.useMapWidgetIds &&
this.props.useMapWidgetIds.length === 1 && (
<JimuMapViewComponent
useMapWidgetIds={this.props.useMapWidgetIds}
onActiveViewChange={this.activeViewChangeHandler}
/>
)
}
</div>
);
}
To display the latitude
and longitude
property of the mouse, the mouse pointer state must be tracked. In the widget.tsx file, add latitude
and longitude
properties to the state object.
state = {
jimuMapView: null,
//*** ADD ***//
latitude: "",
longitude: ""
};
At the top of the file, import the Point
class.
import Point = require("esri/geometry/Point");
In the activeViewChangeHandler
function, just below the setState
command, get the latitude and longitude and update the state every time the pointer-move
event is triggered.
jmv.view.on("pointer-move", evt => {
const point: Point = this.state.jimuMapView.view.toMap({
x: evt.x,
y: evt.y
});
this.setState({
latitude: point.latitude.toFixed(3),
longitude: point.longitude.toFixed(3)
});
});
With that latitude and longitude in the component's state, you can easily display the values in the render
function.
render
function, in the return()
statement (right after the JimuMapViewComponent
placed earlier), add JSX to display the latitude and longitude. render() {
return (
<div className="widget-starter jimu-widget">
{this.props.hasOwnProperty("useMapWidgetIds") &&
this.props.useMapWidgetIds &&
this.props.useMapWidgetIds.length === 1 && (
<JimuMapViewComponent
useMapWidgetIds={this.props.useMapWidgetIds}
onActiveViewChange={this.activeViewChangeHandler}
/>
)
}
{/* *** ADD *** */}
<p>Lat/Lon: {this.state.latitude} {this.state.longitude}</p>
</div>
);
}
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
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.
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.
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.
Add the map's zoom level and scale to the display right next to the latitude and longitude. (Challenge solution)