vue中使用高德地图根据搜索结果添加 marker( 使用 PlaceSearch 进行关键字搜索)

需要注意的是搜索厕所时,传入search方法中的关键词必须加上具体位置,比如具体的景区‘故宫厕所’,才可以,不然就是整个市的厕所

// 厕所
    toiletSelect() {
      var placeSearch = new AMap.PlaceSearch({
        // city 指定搜索所在城市,支持传入格式有:城市名、citycode和adcode
        city: '010', // 兴趣点城市
      });
     // 重点
      placeSearch.search(`${this.drawingMapDetails.scenicName}厕所`, function(status, result) {
        // 查询成功时,result即对应匹配的POI信息
        var pois = result.poiList.pois;
        for (var i = 0; i < pois.length; i++) {
          var poi = pois[i];
          var marker = [];
          marker[i] = new AMap.Marker({
            position: poi.location, // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
            title: poi.name,
            icon: "https://h5.newljlx.com/static/jpjj/cesuo1.png"
          });
          // 将创建的点标记添加到已有的地图实例:
          map.add(marker[i]);
        }
        map.setFitView();
      });
    }
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要清除高德地图搜索结果 Marker,您可以通过以下步骤实现: 1. 在 Vue 组件引入高德地图 JavaScript API: ``` <script src="https://webapi.amap.com/maps?v=1.4.15&key=YOUR_API_KEY"></script> ``` 2. 在 `mounted` 钩子函数初始化地图,并将地图实例保存在 `this.map` : ``` mounted() { this.map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); } ``` 3. 在搜索时,使用 `AMap.Marker` 创建 Marker,并保存在数组: ``` search() { // 清除之前的 Marker this.clearMarkers(); // 搜索 AMap.plugin('AMap.PlaceSearch', () => { const placeSearch = new AMap.PlaceSearch({ city: '北京市', pageSize: 10, pageIndex: 1, map: this.map }); placeSearch.search('餐厅', (status, result) => { if (status === 'complete' && result.info === 'OK') { const poiList = result.poiList; poiList.forEach(poi => { const marker = new AMap.Marker({ position: poi.location, title: poi.name, map: this.map }); this.markers.push(marker); }); } }); }); }, ``` 4. 在清除搜索结果时,遍历 Marker 数组,调用 `setMap(null)` 方法将 Marker 从地图上移除,并清空数组: ``` clearMarkers() { this.markers.forEach(marker => { marker.setMap(null); }); this.markers = []; }, ``` 完整的 Vue 组件代码如下: ``` <template> <div> <div id="map-container"></div> <button @click="search">搜索餐厅</button> <button @click="clearMarkers">清除搜索结果</button> </div> </template> <script> export default { data() { return { map: null, markers: [] }; }, mounted() { this.map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); }, methods: { search() { // 清除之前的 Marker this.clearMarkers(); // 搜索 AMap.plugin('AMap.PlaceSearch', () => { const placeSearch = new AMap.PlaceSearch({ city: '北京市', pageSize: 10, pageIndex: 1, map: this.map }); placeSearch.search('餐厅', (status, result) => { if (status === 'complete' && result.info === 'OK') { const poiList = result.poiList; poiList.forEach(poi => { const marker = new AMap.Marker({ position: poi.location, title: poi.name, map: this.map }); this.markers.push(marker); }); } }); }); }, clearMarkers() { this.markers.forEach(marker => { marker.setMap(null); }); this.markers = []; }, } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值