| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Vue2 高德地图地区查询与绘制</title>
- <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js"></script>
- <script src="https://webapi.amap.com/maps?v=2.0&key=b96a1c32b0bb828f1d153b219fa59ecc&plugin=AMap.ElasticMarker,AMap.PolygonEditor,AMap.DistrictSearch,AMap.ToolBar, AMap.Scale, AMap.MouseTool, AMap.Geocoder,AMap.Zoom"></script>
- <style>
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
- }
- body {
- background-color: #f5f7fa;
- color: #333;
- line-height: 1.6;
- }
- .container {
- max-width: 1200px;
- margin: 0 auto;
- padding: 20px;
- }
- header {
- text-align: center;
- margin-bottom: 30px;
- padding: 20px;
- background: linear-gradient(135deg, #1e5799 0%, #207cca 100%);
- color: white;
- border-radius: 10px;
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
- }
- h1 {
- font-size: 2.2rem;
- margin-bottom: 10px;
- }
- .subtitle {
- font-size: 1.1rem;
- opacity: 0.9;
- }
- .app-container {
- display: flex;
- flex-direction: column;
- gap: 20px;
- }
- .search-panel {
- background: white;
- padding: 20px;
- border-radius: 10px;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
- }
- .search-box {
- display: flex;
- gap: 10px;
- margin-bottom: 15px;
- }
- .search-input {
- flex: 1;
- padding: 12px 15px;
- border: 1px solid #ddd;
- border-radius: 6px;
- font-size: 16px;
- transition: border-color 0.3s;
- }
- .search-input:focus {
- outline: none;
- border-color: #1e5799;
- box-shadow: 0 0 0 2px rgba(30, 87, 153, 0.2);
- }
- .search-btn, .draw-btn, .clear-btn, .edit-btn {
- padding: 12px 20px;
- background: #1e5799;
- color: white;
- border: none;
- border-radius: 6px;
- cursor: pointer;
- font-size: 16px;
- transition: background 0.3s;
- }
- .search-btn:hover, .draw-btn:hover, .edit-btn:hover {
- background: #16457a;
- }
- .clear-btn {
- background: #e74c3c;
- }
- .clear-btn:hover {
- background: #c0392b;
- }
- .edit-btn.active {
- background: #27ae60;
- }
- .map-container {
- height: 500px;
- border-radius: 10px;
- overflow: hidden;
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
- }
- .controls {
- display: flex;
- gap: 10px;
- margin-top: 15px;
- flex-wrap: wrap;
- }
- .results-panel {
- background: white;
- padding: 20px;
- border-radius: 10px;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
- max-height: 200px;
- overflow-y: auto;
- }
- .result-item {
- padding: 10px 15px;
- border-bottom: 1px solid #eee;
- cursor: pointer;
- transition: background 0.2s;
- }
- .result-item:hover {
- background: #f0f7ff;
- }
- .result-item:last-child {
- border-bottom: none;
- }
- .instructions {
- background: white;
- padding: 20px;
- border-radius: 10px;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
- margin-top: 20px;
- }
- .instructions h3 {
- color: #1e5799;
- margin-bottom: 10px;
- }
- .instructions ul {
- padding-left: 20px;
- }
- .instructions li {
- margin-bottom: 8px;
- }
- .status {
- margin-top: 10px;
- padding: 10px;
- border-radius: 6px;
- background: #e8f4fd;
- color: #1e5799;
- }
- .drawing-status {
- background: #fff8e1;
- color: #ff9800;
- }
- .editing-status {
- background: #e8f6f3;
- color: #27ae60;
- }
- @media (min-width: 768px) {
- .app-container {
- flex-direction: row;
- }
- .search-panel {
- width: 300px;
- flex-shrink: 0;
- }
- .map-container {
- flex: 1;
- }
- }
- </style>
- </head>
- <body>
- <div id="app" class="container">
- <header>
- <h1>Vue2 高德地图地区查询与绘制</h1>
- <p class="subtitle">搜索地区并在高德地图上绘制区域边界</p>
- </header>
- <div class="app-container">
- <div class="search-panel">
- <h3>地区搜索</h3>
- <div class="search-box">
- <input
- type="text"
- class="search-input"
- placeholder="输入地区名称,如:北京市朝阳区"
- v-model="searchKeyword"
- @keyup.enter="searchPlace"
- >
- <button class="search-btn" @click="searchPlace">搜索</button>
- </div>
- <div class="controls">
- <button class="draw-btn" @click="toggleDrawingMode">
- {{ drawingMode ? '退出绘制模式' : '进入绘制模式' }}
- </button>
- <button class="edit-btn" :class="{active: editingMode}" @click="toggleEditingMode">
- {{ editingMode ? '退出编辑模式' : '进入编辑模式' }}
- </button>
- <button class="clear-btn" @click="clearMap">清除地图</button>
- </div>
- <div class="status" :class="statusClass">
- {{ statusMessage }}
- </div>
- <div class="results-panel" v-if="searchResults.length > 0">
- <h4>搜索结果 ({{ searchResults.length }})</h4>
- <div
- class="result-item"
- v-for="(result, index) in searchResults"
- :key="index"
- @click="selectResult(result)"
- >
- <strong>{{ result.name }}</strong><br>
- <small>{{ result.district }}{{ result.address }}</small>
- </div>
- </div>
- </div>
- <div class="map-container" id="map-container"></div>
- </div>
- <div class="instructions">
- <h3>使用说明</h3>
- <ul>
- <li>在搜索框中输入地区名称(如"北京市朝阳区")并点击搜索或按Enter键</li>
- <li>点击搜索结果项可在地图上定位到该地区</li>
- <li>点击"进入绘制模式"可手动在地图上绘制多边形区域</li>
- <li>绘制完成后,点击第一个点或双击可完成绘制</li>
- <li>点击"进入编辑模式"可选择并编辑已绘制的区域</li>
- <li>点击"清除地图"可移除所有标记和绘制区域</li>
- </ul>
- </div>
- </div>
- <script type="text/javascript">
- window._AMapSecurityConfig = {
- serviceHost: 'http://47.110.79.22:9000/_AMapService',
- }
- </script>
- <script>
- // 请替换为您的高德地图API密钥
- const AMAP_KEY = 'b96a1c32b0bb828f1d153b219fa59ecc';
- new Vue({
- el: '#app',
- data: {
- map: null,
- searchKeyword: '',
- searchResults: [],
- drawingMode: false,
- editingMode: false,
- mouseTool: null,
- polygonEditor: null,
- polygons: [],
- markers: [],
- selectedPolygon: null,
- statusMessage: '请输入地区名称进行搜索',
- statusClass: ''
- },
- computed: {
- statusClass() {
- if (this.drawingMode) return 'drawing-status';
- if (this.editingMode) return 'editing-status';
- return '';
- }
- },
- mounted() {
- this.initMap();
- },
- methods: {
- initMap() {
- // 初始化地图
- this.map = new AMap.Map('map-container', {
- zoom: 10,
- center: [116.397428, 39.90923], // 默认中心点(北京)
- viewMode: '3D'
- });
- // 添加缩放控件
- // this.map.addControl(new AMap.Zoom());
- // 添加比例尺控件
- this.map.addControl(new AMap.Scale());
- // 初始化鼠标工具
- this.mouseTool = new AMap.MouseTool(this.map);
- // 监听绘制完成事件
- this.mouseTool.on('draw', (event) => {
- if (event.obj instanceof AMap.Polygon) {
- const polygon = event.obj;
- this.polygons.push(polygon);
- // 添加右键菜单删除功能
- this.addPolygonContextMenu(polygon);
- this.statusMessage = `已绘制区域,顶点数: ${polygon.getPath().length}`;
- // 自动退出绘制模式
- this.drawingMode = false;
- }
- });
- this.statusMessage = '地图加载完成,请输入地区名称进行搜索';
- },
- searchPlace() {
- if (!this.searchKeyword.trim()) {
- this.statusMessage = '请输入搜索关键词';
- return;
- }
- this.statusMessage = '搜索中...';
- // 使用高德地图PlaceSearch插件进行搜索
- AMap.plugin('AMap.PlaceSearch', () => {
- const placeSearch = new AMap.PlaceSearch({
- pageSize: 10,
- pageIndex: 1,
- citylimit: false
- });
- placeSearch.search(this.searchKeyword, (status, result) => {
- if (status === 'complete' && result.poiList && result.poiList.pois) {
- this.searchResults = result.poiList.pois;
- this.statusMessage = `找到 ${this.searchResults.length} 个结果`;
- } else {
- this.searchResults = [];
- this.statusMessage = '未找到相关结果,请尝试其他关键词';
- }
- });
- });
- },
- selectResult(result) {
- // 清除之前的标记
- this.clearMarkers();
- // 添加标记
- const marker = new AMap.Marker({
- position: [result.location.lng, result.location.lat],
- title: result.name,
- map: this.map
- });
- this.markers.push(marker);
- // 移动到标记位置
- this.map.setZoom(15);
- this.map.setCenter([result.location.lng, result.location.lat]);
- this.statusMessage = `已定位到: ${result.name}`;
- },
- toggleDrawingMode() {
- if (this.editingMode) {
- this.toggleEditingMode(); // 先退出编辑模式
- }
- this.drawingMode = !this.drawingMode;
- if (this.drawingMode) {
- // 进入绘制模式
- this.mouseTool.polygon({
- strokeColor: '#1e5799',
- strokeOpacity: 1,
- strokeWeight: 3,
- fillColor: '#1e5799',
- fillOpacity: 0.3,
- strokeStyle: 'solid'
- });
- this.statusMessage = '绘制模式:请在地图上点击绘制多边形区域,双击或点击第一个点完成绘制';
- } else {
- // 退出绘制模式
- this.mouseTool.close(true);
- this.statusMessage = '已退出绘制模式';
- }
- },
- toggleEditingMode() {
- if (this.drawingMode) {
- this.toggleDrawingMode(); // 先退出绘制模式
- }
- this.editingMode = !this.editingMode;
- if (this.editingMode) {
- this.statusMessage = '编辑模式:请点击要编辑的多边形区域';
- // 为所有多边形添加点击事件
- this.polygons.forEach(polygon => {
- polygon.on('click', () => {
- this.startEditPolygon(polygon);
- });
- });
- } else {
- this.stopEditPolygon();
- this.statusMessage = '已退出编辑模式';
- }
- },
- startEditPolygon(polygon) {
- // 停止之前的编辑
- this.stopEditPolygon();
- // 开始编辑选中的多边形
- this.selectedPolygon = polygon;
- // 加载PolyEditor插件
- AMap.plugin('AMap.PolyEditor', () => {
- this.polygonEditor = new AMap.PolyEditor(this.map, polygon);
- this.polygonEditor.open();
- this.statusMessage = '正在编辑多边形,拖动顶点或边进行调整';
- // 监听编辑事件
- this.polygonEditor.on('adjust', (event) => {
- this.statusMessage = '正在调整多边形顶点';
- });
- this.polygonEditor.on('end', (event) => {
- this.statusMessage = `编辑完成,当前顶点数: ${polygon.getPath().length}`;
- });
- });
- },
- stopEditPolygon() {
- if (this.polygonEditor) {
- this.polygonEditor.close();
- this.polygonEditor = null;
- }
- this.selectedPolygon = null;
- // 移除多边形的点击事件
- this.polygons.forEach(polygon => {
- polygon.off('click');
- });
- },
- addPolygonContextMenu(polygon) {
- // 添加右键菜单删除功能
- polygon.on('rightclick', (e) => {
- // 创建右键菜单
- const menu = new AMap.ContextMenu();
- menu.addItem("删除此区域", () => {
- // 从地图移除
- polygon.setMap(null);
- // 从数组中移除
- const index = this.polygons.indexOf(polygon);
- if (index > -1) {
- this.polygons.splice(index, 1);
- }
- this.statusMessage = '已删除选中区域';
- // 如果正在编辑此多边形,停止编辑
- if (this.selectedPolygon === polygon) {
- this.stopEditPolygon();
- this.editingMode = false;
- }
- }, 0);
- // 在点击位置打开菜单
- menu.open(this.map, e.lnglat);
- });
- },
- clearMap() {
- // 清除所有标记
- this.clearMarkers();
- // 清除所有多边形
- this.clearPolygons();
- // 退出绘制和编辑模式
- if (this.drawingMode) {
- this.drawingMode = false;
- this.mouseTool.close(true);
- }
- if (this.editingMode) {
- this.editingMode = false;
- this.stopEditPolygon();
- }
- this.statusMessage = '地图已清除';
- },
- clearMarkers() {
- this.markers.forEach(marker => {
- marker.setMap(null);
- });
- this.markers = [];
- },
- clearPolygons() {
- this.polygons.forEach(polygon => {
- polygon.setMap(null);
- });
- this.polygons = [];
- }
- }
- });
- </script>
- </body>
- </html>
|