2

I try to connect Leaflet search plugin to my map with GeoJSON data. But when I enter a search query in browser inspector I get error: layer.getBounds is not a function in leaflet-search.js. You can see it on http://ignn.ru/test/ (try to input "Volgograd" or "Alaska")

var searchControl = new L.Control.Search({
        layer: ggg,
        propertyName: 'name',
        marker: false,
        moveToLocation: function(latlng, title, map) {
            //map.fitBounds( latlng.layer.getBounds() );
            var zoom = map.getBoundsZoom(latlng.layer.getBounds());
            map.setView(latlng, zoom); // access the zoom
        }
    });
map.addControl( searchControl );

I did this on example http://labs.easyblog.it/maps/leaflet-search/examples/geojson-layer.html

ggg: layer with geojson data

I understand because of this error the reason that do not give you any search results.

GeoJSON data - http://ignn.ru/test/test.js

Update: In data there is a single object of type point. When I remove it, everything works. But I need the elements of type point.

1

1 Answer 1

2

Here I load the search control, define the layer to search, the field to search, and then just use setView to zoom to the point. I set the zoom to 12 for grins. Since it's a point, I just set the zoom for all my points the same.

http://www.gistechsolutions.com/leaflet/DEMO/baseball/Baseball2.html

    var searchControl = new L.Control.Search({
    layer: bbTeam,
    propertyName: 'Name',
    marker: false,
    moveToLocation: function(latlng) {
        map.setView(latlng, 12); // set the zoom
    }
});
7
  • Bill Chappell, Hm..Thank you! Tell me please. I have name in json for example (Volgograd City, Lenin street 10). and if i input Volgograd (first word) i have result. But when i input City or Lenin i not get result( Why?
    – technor
    Commented Feb 12, 2018 at 19:43
  • I looked at your geojson data and you have points and multipolygons in the same file under the var statesData, yet your var ggg is only using points 'circlemarkers', so don't have a polygon layer in your example to symbolize. As for the search only returning just by name, in your GeoJSON file I found "name":"Volgograd" which matched just the first name, so it is matching your data. You mention an address but your not using a geocoder, so you won't get street addresses. Commented Feb 13, 2018 at 13:09
  • Hello again =) Bill just look at ignn.ru/map, geojson at ignn.ru/map/hs.js. Fox example - name = г. Волгоград п. 19 Партсъезда пер. Банный д. 6. By first word "г.Волгоград" i get result. But.for example by next someone word like "Партсъезда" search pulgin don't found result =(
    – technor
    Commented Feb 13, 2018 at 13:37
  • 1
    I looked at your GeoJSON and the first name was " обл. Волгоградская, г. Волгоград, п. 19 Партсъезда, пер. Банный, д. 6" I couldn't find any names that matched your name, I'm guessing it's language/translate error. It may also be a data problem, I checked your data on jsonlint.com and geojsonlint.com both showed errors. In my example my JSON is:gistechsolutions.com/leaflet/DEMO/baseball/BaseBallFinal.json and I use JQuery's $.getJSON to bring in the data, Try this. In my example type "B" and it should suggest. Commented Feb 13, 2018 at 14:23
  • gistechsolutions.com/leaflet/DEMO/baseball/Baseball2.html In this example have marker with name "White sox". When i type W or White i get result. But when i type sox i don`t get result..
    – technor
    Commented Feb 13, 2018 at 18:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.