如何过滤不同颜色的Map标记类别传单和显示弹出从API使用Vue.JS

jtoj6r0c  于 2023-01-21  发布在  Vue.js
关注(0)|答案(1)|浏览(84)

我有一个案例,所以我想根据API中使用Vue.js框架中的Leaflet map的聚类图类别过滤,给予标记颜色,如何操作?

46qrfjad

46qrfjad1#

首先需要安装依赖项
1.传单。

  1. leaflet.markercluster.
    1.传单-面板-层。
    然后像这样导入
import L from 'leaflet';
import axios from 'axios';
import 'leaflet.markercluster/dist/MarkerCluster.css';
import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
import markerClusterGroup from 'leaflet.markercluster/dist/leaflet.markercluster';
import 'leaflet/dist/leaflet.css';
import 'leaflet-search/dist/leaflet-search.min.css';
import LControlSearch from 'leaflet-search';

你需要将标记安装到mounted

this.markerClusterGroup = L.markerClusterGroup()

将此代码放入mounted

axios.get(`{https://localhost:3030/api/company/locations}`)
.then(response => {
  // Add the markers to the map
  let locations = response.data.data.content;
  locations.forEach(marker => {
    let m = L.marker([marker.latitude, marker.longitude])
    if (marker.type_cluster === "Military") {
      m.setIcon(L.icon({
        iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png',
        shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
        iconSize: [25, 41],
        iconAnchor: [12, 41],
        popupAnchor: [1, -34],
        shadowSize: [41, 41]
      }))
      m.bindPopup("Company Name : "+marker.name_company + "<br> Tipe Organisasi : " + marker.type_cluster + "<br>" + "Phone Number : " + marker.contact_number + "")
      this.markerClusterGroup.addLayer(m)
    } else if (marker.type_cluster === "Campus") {
      m.setIcon(L.icon({
        iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png',
        shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
        iconSize: [25, 41],
        iconAnchor: [12, 41],
        popupAnchor: [1, -34],
        shadowSize: [41, 41]
      }))
      m.bindPopup("Company Name : "+marker.name_company + "<br> Tipe Organisasi : " + marker.type_cluster + "<br>" + "Phone Number : " + marker.contact_number + "")
      this.markerClusterGroup.addLayer(m)
    } else if (marker.type_cluster === "Others") {
      m.setIcon(L.icon({
        iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-blue.png',
        shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
        iconSize: [25, 41],
        iconAnchor: [12, 41],
        popupAnchor: [1, -34],
        shadowSize: [41, 41]
      }))
      m.bindPopup("Company Name : "+marker.name_company + "<br> Tipe Organisasi : " + marker.type_cluster + "<br>" + "Phone Number : " + marker.contact_number + "")
      this.markerClusterGroup.addLayer(m)
    }
  });
})

希望这对你有帮助

相关问题