0

I have a function to search from layer. I got the desired results, so I want to zoom-in and highlight the results I found. How can I do that?

This is my code:

var sothuaMap = document.getElementById('sothuaMap');
    var sotoMap = document.getElementById('sotoMap');
    var phuongMap = document.getElementById('phuongMap');
    var searchButton = document.getElementById('search');
    var resultSpan = document.getElementById('span');

    function search(sothuaGet, sotoGet, phuongMap) {
        resultSpan.innerHTML = 'Searching...';
        fetch('http://localhost:8081/geoserver/vietnam/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=vietnam%3Ahtgovap&maxFeatures=50000&outputFormat=application%2Fjson&q=' + sothuaGet)
          .then(function (response) {
            return response.json();
          })
          .then(function (json) {
            var results = json.features;
            // console.log(results);
            if (results && results.length > 0) {
                for (let i = 0, ii = results.length; i < ii; i++) {
                    const result = results[i];
                    if(result){
                        var props = result.properties; //get properties
                        var geom = result.geometry; //get geometry

                        var sothua = props.shthua;
                        var soto = props.shbando;
                        var phuong = props.xaphuong;
                        var dientich = props.dientich;
                        var coordinate = geom.coordinates;
                        if(sothua == sothuaGet && soto == sotoGet &&  phuong == phuongMap){
                            setProjection(sothua,soto,dientich,coordinate);
                            return;
                        }
                    }
                }
            }
            // setProjection(null, null, null);
          });
      }

    function setProjection(sothua, soto, dientich, coordinate){
        //
        if(sothua == null || soto == null){
            resultSpan.innerHTML = 'Not found, try again...!'
            return
        }
        resultSpan.innerHTML = 'ST: ' + sothua  + ' ' 
                                + 'SHBD: ' + soto +' '
                                + 'DT: ' + dientich + '<br>' 
                                + 'Geom: ' + coordinate;    
    }

    searchButton.onclick = function (event) {
        if(sotoMap.value != '' && sothuaMap.value != '' && phuongMap.value != ''){
            search(sothuaMap.value, sotoMap.value, phuongMap.value);
            event.preventDefault();
        }else{
            alert('Input data empty!');
            event.preventDefault();
        }
    };

"Highlight" the same this:

enter image description here

6
  • I suppose final result is displayed with setProjection function, so zoom should happen there. Is this correct? Also what do you mean by "highligh result"? Please edit your question and add info about that.
    – TomazicM
    Commented Aug 16, 2022 at 12:04
  • Yes, that right! Result is displayed at setProjection function. I mean highlight means make it stand out
    – Jayzz
    Commented Aug 16, 2022 at 12:47
  • For highlighting the feature you need one of two things: (1) either some property in returned search result by which you can identify feature on the map and change it's style (2) or search result must contain geometry with which you create highlighted feture ans show it on the map.
    – TomazicM
    Commented Aug 16, 2022 at 13:20
  • How can I do that, can you give me an example? And I also want to zoom in to the highlighted feature. Thanks you.
    – Jayzz
    Commented Aug 16, 2022 at 13:29
  • 2
    Thee response from the WFS is a GeoJSON. You can parse it to add the geometry to the map and zoom in on it codesandbox.io/s/simple-forked-9yqzh5?file=/main.js
    – Mike
    Commented Aug 16, 2022 at 20:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.