I am very new to JavaScript SDK. I can't figure out why I have 2 maps in my view.
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map using arcgis/map-components</title>
<style>
html,
body {
height: 100%;
margin: 0;
}
#searchDiv {
position: absolute;
top: 10px;
left: 60px;
z-index: 99;
}
</style>
<!-- Load Calcite components from CDN -->
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
<!-- Load Map components from CDN-->
</head>
<body>
<div id="searchDiv"></div>
<arcgis-map id="arcgisMap" item-id="8914d03ca2d745bea3beec1155d0614b">
<arcgis-zoom position="top-left"></arcgis-zoom>
</arcgis-map>
<script>
require([
"esri/views/MapView",
"esri/WebMap",
"esri/widgets/Search",
"esri/layers/FeatureLayer",
"esri/rest/support/Query"
], function (MapView, WebMap, Search, FeatureLayer, Query) {
const webmap = new WebMap({
portalItem: { id: "8914d03ca2d745bea3beec1155d0614b" }
});
const view = new MapView({
container: "arcgisMap",
map: webmap
});
// Add Search widget
const searchWidget = new Search({
view: view,
container: "searchDiv"
});
// Once the map loads, get the first operational layer
view.when(async () => {
await view.whenLayerView(webmap.layers.getItemAt(0));
const firstLayer = webmap.layers.getItemAt(0);
searchWidget.on("select-result", (event) => {
const resultGeom = event.result.feature.geometry;
const query = new Query({
geometry: resultGeom,
spatialRelationship: "intersects",
outFields: ["*"],
returnGeometry: true
});
firstLayer.queryFeatures(query).then((result) => {
if (result.features.length > 0) {
const feature = result.features[0];
const attrs = feature.attributes;
// Custom popup formatting
const popupContent = `
<b>Precinct:</b> ${attrs.PCT_NO}<br>
<b>Commissioner:</b> ${attrs.COMMISSION}<br>
<b>Website:</b> <a href="${attrs.URL}" target="_blank">View Site</a>
`;
view.popup.open({
title: "Commissioner Precinct",
content: popupContent,
location: resultGeom
});
} else {
view.popup.open({
title: "No features found",
location: resultGeom
});
}
});
});
});
});
</script>
</body>
</html>