0

I using search nominatim function from ol-ext library. I want to get extent from search results of county or city when selecting but I haven't found the solution yet.

This is my code:

update

// Current selection
var sLayer = new ol.layer.Vector({
    source: new ol.source.Vector(),
    style: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 5,
            stroke: new ol.style.Stroke({
                color: 'rgb(255,165,0)',
                width: 3
            }),
            fill: new ol.style.Fill({
                color: 'rgba(255,165,0,.3)'
            })
        }),
        stroke: new ol.style.Stroke({
            color: 'rgb(255,165,0)',
            width: 3
        }),
        fill: new ol.style.Fill({
            color: 'rgba(255,165,0,.3)'
        })
    })
});
map.addLayer(sLayer);
// Set search control
searchNomina = new ol.control.SearchNominatim({
    reverse: true,
    polygon: true,  //include polygon
    // target: $("#asd > div").get(0),
    placeholder: 'Street name...',
    // className: "tim ol-unselectable ol-control",
    position: true  // Search, with priority to geo position
});
var requestData = searchNomina.requestData.bind(searchNomina);
searchNomina.requestData = function (e) {
    var data = requestData(e);
    data.countrycodes = 'en';
    return data;
};
map.addControl(searchNomina);
// Select feature when click
searchNomina.on('select', function (e) {
    // console.log(e);
    sLayer.getSource().clear();
    // Check
    if (e.search.geojson) {
        var fort = new ol.format.GeoJSON();
        var parser = fort.readFeature(e.search.geojson, { dataProjection: "EPSG:4326", featureProjection: map.getView().getProjection() });
        sLayer.getSource().addFeature(parser);
        var view = map.getView();
        var resolution = view.getResolutionForExtent(fort.getGeometry().getExtent(), map.getSize());
        var zoom = view.getZoomForResolution(resolution);
        var center = ol.extent.getCenter(fort.getGeometry().getExtent());
        console.log(center, zoom, resolution, view);
        // redraw before zoom
        console.log('aaaa');
        setTimeout(function () {
            console.log('cccc');
            view.animate({
                center: center,
                zoom: Math.min(zoom, 16)
            });
        }, 100);
    } else {
        map.getView().animate({
            center: e.coordinate,
            zoom: Math.max(map.getView().getZoom(), 16)
        });
        var extentDist = sLayer.getSource().getExtent();
        console.log(extentDist);
    }
});
2
  • 1
    You need to include polygon:: true, in the ol.control.SearchNominatim options to get a polygon geometry returned.
    – Mike
    Feb 7, 2023 at 14:27
  • Thanks Mike, it really helped me.
    – Jayzz
    Feb 8, 2023 at 2:32

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.