How to add a pointer or a marker on a map using coordinates?

104
2
04-07-2020 12:21 PM
Highlighted
New Contributor III

I want to add a pointer or a marker on a webmap using coordinates. Below is the code to my original map, it works:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<title>Load a basic WebMap - 4.14</title>

<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>

<link
rel="stylesheet"
href="https://js.arcgis.com/4.14/esri/themes/light/main.css"
/>

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

<script>
require(["esri/views/MapView", "esri/WebMap", "esri/config", "esri/widgets/Sketch", "esri/layers/GraphicsLayer"], function(MapView, WebMap, esriConfig, Sketch, GraphicsLayer) {
/************************************************************
* Creates a new WebMap instance. A WebMap must reference
* a PortalItem ID that represents a WebMap saved to
* arcgis.com or an on-premise portal.
*
* To load a WebMap from an on-premise portal, set the portal
* url with esriConfig.portalUrl.
************************************************************/
esriConfig.portalUrl = "https://portal.environment.gov.za/portal";

var webmap = new WebMap({
portalItem: {
// autocasts as new PortalItem()
id: "04582be14885483da48f29398960f653"
}

});

var graphicsLayer = new GraphicsLayer();

/************************************************************
* Set the WebMap instance to the map property in a MapView.
************************************************************/
var view = new MapView({
map: webmap,
container: "viewDiv"
});

webmap.layers.add(graphicsLayer);

var sketch = new Sketch({
layer: graphicsLayer,
view: view,
//container: "viewDiv"
});

view.ui.add(sketch, {
position: "top-right"
});
// Listen to sketch widget's create event.
sketch.on("create", function(event) {
// check if the create event's state has changed to complete indicating
// the graphic create operation is completed.
console.log(view.zoom);
if (event.state === "complete") {
// remove the graphic from the layer. Sketch adds
// the completed graphic to the layer by default.
graphicsLayer.remove(event.graphic);

// use the graphic.geometry to query features that intersect it
selectFeatures(event.graphic.geometry);
}
});
});
</script>
</head>

<body>
<div id="viewDiv"></div>
</body>
</html>

And this is the code I am using trying to add the pointer or a marker on the webmap but it is not working, can you please advice me on what am I doing wrong?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<title>Load a basic WebMap - 4.14</title>

<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>

<link
rel="stylesheet"
href="https://js.arcgis.com/4.14/esri/themes/light/main.css"
/>

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

<script>
require(["esri/views/MapView",
"esri/WebMap",
"esri/config",
"esri/widgets/Sketch",
"esri/Graphic",
"esri/layers/GraphicsLayer"
], function(MapView, Graphic, WebMap, esriConfig, Sketch, GraphicsLayer) {
/************************************************************
* Creates a new WebMap instance. A WebMap must reference
* a PortalItem ID that represents a WebMap saved to
* arcgis.com or an on-premise portal.
*
* To load a WebMap from an on-premise portal, set the portal
* url with esriConfig.portalUrl.
************************************************************/
esriConfig.portalUrl = "https://portal.environment.gov.za/portal";

var webmap = new WebMap({
portalItem: {
// autocasts as new PortalItem()
id: "04582be14885483da48f29398960f653"
}

});

/************************************************************
* Set the WebMap instance to the map property in a MapView.
************************************************************/
var view = new MapView({
map: webmap,
container: "viewDiv"
center:[ 28.159843448357865, -26.374449292814024],
zoom: 14
});

var graphicsLayer = new GraphicsLayer();

webmap.layers.add(graphicsLayer);

// Create a point
var point = {
type: "point",
longitude: 28.159843448357865,
latitude: -26.374449292814024
};

var simpleMarkerSymbol = {
type: "simple-maker",
color: [226, 119, 40], // orange
size: 30,
style: "circle",
outline: {
color: [255, 255, 255], // white
width: 2
}
};

var pointGraphic = new Graphic({
geometry: point,
symbol: simpleMarkerSymbol
});
graphicsLayer.add(pointGraphic);

var sketch = new Sketch({
layer: graphicsLayer,
view: view,
//container: "viewDiv"
});

view.ui.add(sketch, {
position: "top-right"
});
// Listen to sketch widget's create event.
sketch.on("create", function(event) {
// check if the create event's state has changed to complete indicating
// the graphic create operation is completed.
console.log(view.zoom);
if (event.state === "complete") {
// remove the graphic from the layer. Sketch adds
// the completed graphic to the layer by default.
graphicsLayer.remove(event.graphic);

// use the graphic.geometry to query features that intersect it
selectFeatures(event.graphic.geometry);
}
});
});
</script>
</head>

<body>
<div id="viewDiv"></div>
</body>
</html>

Reply
0 Kudos
2 Replies
Highlighted
New Contributor III

the first code is working, the second one is not

Reply
0 Kudos
Highlighted
Occasional Contributor II

The module require needs to match up with those in the function. So you have

require(["esri/views/MapView",
"esri/WebMap",
"esri/config",
"esri/widgets/Sketch",
"esri/Graphic",
"esri/layers/GraphicsLayer"
], function(MapView, Graphic, WebMap, esriConfig, Sketch, GraphicsLayer) {

try changing it to

require(["esri/views/MapView",
"esri/WebMap",
"esri/Graphic",
"esri/config",
"esri/widgets/Sketch",
"esri/layers/GraphicsLayer"
], function(MapView, WebMap, Graphic, esriConfig, Sketch, GraphicsLayer) {