Ujang Aripin
Ujang Aripin

Reputation: 350

How to filter different color map markers Category leaflet and show popup from API using Vue.JS?

I have a case, so I want to give a color to marker according to filtered with cluster map category from an API using the Leaflet map in Vue.js framework. How can I do this?

Upvotes: 0

Views: 172

Answers (1)

Ujang Aripin
Ujang Aripin

Reputation: 350

first you need install Dependency

  1. leaflet.
  2. leaflet.markercluster.
  3. leaflet-panel-layers.

and Import it like this

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';

you need install the markers into mounted

this.markerClusterGroup = L.markerClusterGroup()

put this code into 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)
    }
  });
})

I hope this help you

Upvotes: 1

Related Questions