PopUp和Graphics的使用
实现功能
实现了鼠标点击后出现点图层并且弹出信息模板。
相关代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="description" content="[Get started with popups - 4.3]">
<!--
ArcGIS API for JavaScript, https://js.arcgis.com
For more information about the get-started-popup sample, read the original sample description at developers.arcgis.com.
https://developers.arcgis.com/javascript/latest/get-started-popup/index.html
-->
<title>Get started with popups - 4.3</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
#instruction {
z-index: 99;
position: absolute;
top: 15px;
left: 50%;
padding: 5px;
margin-left: -175px;
height: 20px;
width: 350px;
background: rgba(25, 25, 25, 0.8);
color: white;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.3/esri/css/main.css">
<script src="https://js.arcgis.com/4.3/"></script>
<script>
require([
"esri/tasks/Locator",
"esri/Map",
"esri/views/MapView",
"esri/symbols/SimpleMarkerSymbol",
"esri/Graphic",
"esri/geometry/Point",
"dojo/domReady!"
], function(
Locator,
Map,
MapView,
SimpleMarkerSymbol,
Graphic,
Point
) {
// Set up a locator task using the world geocoding service
var locatorTask = new Locator({
url: "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"
});
//add a graphic layer
// Create the Map
var map = new Map({
basemap: "streets-navigation-vector"
});
// Create the MapView
var view = new MapView({
container: "viewDiv",
map: map,
center: [-116.3031, 43.6088],
zoom: 12
});
function addPoint(x,y){
if(!view.isRejected()){
view.graphics.removeAll();
}
let point = new Point({
longitude:x,
latitude: y
}),
markerSymbol = new SimpleMarkerSymbol({
color: [226, 119, 40],
outline: { // autocasts as new SimpleLineSymbol()
color: [255, 255, 255],
width: 2
}
}),
// Create a graphic and add the geometry and symbol to it
pointGraphic = new Graphic({
geometry: point,
symbol: markerSymbol
});
view.graphics.addMany([pointGraphic]);
}
/*******************************************************************
* This click event sets generic content on the popup not tied to
* a layer, graphic, or popupTemplate. The location of the point is
* used as input to a reverse geocode method and the resulting
* address is printed to the popup content.
*******************************************************************/
view.on("click", function(event) {
// Get the coordinates of the click on the view
var lat = Math.round(event.mapPoint.latitude * 1000) / 1000;
var lon = Math.round(event.mapPoint.longitude * 1000) / 1000;
addPoint(lon,lat);
view.popup.open({
// Set the popup's title to the coordinates of the location
title: "Reverse geocode: [" + lon + ", " + lat + "]",
location: event.mapPoint // Set the location of the popup to the clicked location
});
// Display the popup
// Execute a reverse geocode using the clicked location
locatorTask.locationToAddress(event.mapPoint).then(function(
response) {
// If an address is successfully found, print it to the popup's content
var address = response.address.Match_addr;
view.popup.content = address;
}).otherwise(function(err) {
// If the promise fails and no result is found, print a generic message
// to the popup's content
view.popup.content =
"No address was found for this location";
});
});
});
</script>
</head>
<body>
<div id="viewDiv"></div>
<div id="instruction">Click any location on the map to see its street address</div>
</body>
</html>
实现效果展示
1-1
1-2
如果读者你细心的话
发现两个效果图的不同点:
告诉你原因,很简单,博主小小的写了一个判断:
if(!view.isRejected()){
view.graphics.removeAll();
}
就这样,一个简单的练手搞定!
在GIS的沙场,传播有价值的东西!