简体   繁体   中英

How to get the nearest Feature of an leaflet WMS Map?

I am quiet new to Web Services and I am facing a problem. I have a Map linked with a WMS. I added a basemap and my WMS Date as a tileLayer and the show up correctly on my map. Now I want the nearest Feature to PopUp when I click anywhere on the map. I already searched the Web but could not find a solution I understood. ( https://gist.github.com/rclark/6908938 ) Can anyone help me with this?

This is my code so far:

    <!DOCTYPE html>
<html>

<head>

    <title>XXX</title>

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
   integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
   crossorigin=""/>

    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
   integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
   crossorigin=""></script>
   <style type="text/css">
    #nrwMap { height: 800px; width: 1300px;}
   </style>

</head>

<body>

     <div id="Map"></div>

     <script type="text/javascript">

        var mymap = L.map('Map').setView([51.28, 7.33], 8);

        var url = 'http://www.xxx.xxx.com/wms/';

        var basemap = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
            '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        id: 'mapbox/streets-v11'
        })

        basemap.addTo(mymap);

        var featureLayer = L.tileLayer.wms(url, {
            layers: 'layerName',
            format: 'image/png',
            transparent: true
        });

        featureLayer.addTo(mymap);

     </script>

</body>

</html>

Leaflet.GeometryUtil has a function/feature called closestLayer. It might help.

mymap.on('click', onMapClick);
function onMapClick(e) {
    nearestLayer = L.GeometryUtil.closestLayer(mymap, layersToSearch, e.latlng)
    // It will return the layer, latlng of point on layer nearest to the 'click' and distance from the 'click'  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM