基于 ArcGIS JS API 的点查询搜索框代码
时间: 2024-02-26 17:54:46 浏览: 88
一个用js实现的页内搜索代码
5星 · 资源好评率100%
以下是一个基于 ArcGIS JS API 的点查询搜索框的代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>点查询搜索框</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.16/esri/themes/light/main.css">
<script src="https://js.arcgis.com/4.16/"></script>
<style>
#viewDiv {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.search {
position: absolute;
z-index: 2;
top: 20px;
left: 20px;
background-color: white;
border-radius: 5px;
padding: 10px;
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.3);
}
.search input {
width: 200px;
padding: 5px;
border-radius: 5px;
border: none;
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.3);
}
.search button {
background-color: #0079c1;
color: white;
padding: 5px 10px;
border-radius: 5px;
border: none;
margin-left: 5px;
cursor: pointer;
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.3);
}
.search button:hover {
background-color: #005fa3;
}
</style>
</head>
<body>
<div id="viewDiv"></div>
<div class="search">
<input type="text" id="searchInput" placeholder="输入关键字搜索">
<button id="searchButton">搜索</button>
</div>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/widgets/Search"
], function(Map, MapView, FeatureLayer, Search) {
// 创建一个地图对象
var map = new Map({
basemap: "gray"
});
// 创建一个地图视图对象
var view = new MapView({
container: "viewDiv",
map: map,
center: [-118.244, 34.052],
zoom: 12
});
// 创建一个要素图层对象
var featureLayer = new FeatureLayer({
url: "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/LA_County_cities/FeatureServer/0"
});
// 添加要素图层到地图中
map.add(featureLayer);
// 创建一个搜索组件对象
var searchWidget = new Search({
view: view
});
// 将搜索组件添加到页面上的搜索框中
searchWidget.container = document.getElementById("searchInput");
// 创建搜索按钮的点击事件
document.getElementById("searchButton").addEventListener("click", function() {
// 获取搜索框中的关键字
var searchValue = document.getElementById("searchInput").value;
// 设置要素图层的查询参数
featureLayer.definitionExpression = "CITY_NAME LIKE '%" + searchValue + "%'";
});
});
</script>
</body>
</html>
```
运行该示例,即可在页面上看到一个包含搜索框的地图界面。当输入关键字并点击搜索按钮时,代码会根据输入的关键字在要素图层中进行查询,并将查询结果显示在地图上。
阅读全文