question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Searchbar for cities

See original GitHub issue

Hi

I displayed on the map an important list of suppliers all over the country (France), I need to search for cities and zoom in in order to see which suppliers are around.

So beside my list of suppliers I got a json list of all cities of France with names and coordinates, I display them with the search plugin, and add layer control in order to hide the markers.


with open('villes.json') as fp:
   points = ``json.load(fp)
       
geojson_obj = folium.GeoJson(points, show=False).add_to(m)
plugins.Search(geojson_obj, geom_type='Point', placeholder="Search", collapsed=True, search_label='Ville', search_zoom=20, position='topleft').add_to(m)
folium.LayerControl().add_to(m)
m.save(map.html)
        

But I face two problems :

  • As they are about 35k cities, the map takes some much time to load
  • We still have the option to show markers in the LayerControl, and if it is activated, this is the end…

Could it be possible to solve both problems ?

Or could it be possible to add external search plugin from Openstreetmaps for instance?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ausiasdribiacommented, Jun 10, 2021

With a colleague’s help, I’ve managed to alter the Geocoder class in order to use the Google Maps geocoding service. I’ll leave the code here just in case someone has the same problem.

class GGeocoder(folium.plugins.Geocoder):
    """A simple geocoder for Leaflet that by default uses OSM/Nominatim.

    Please respect the Nominatim usage policy:
    https://operations.osmfoundation.org/policies/nominatim/

    Parameters
    ----------
    collapsed: bool, default False
        If True, collapses the search box unless hovered/clicked.
    position: str, default 'topright'
        Choose from 'topleft', 'topright', 'bottomleft'
        or 'bottomright'.
    add_marker: bool, default True
        If True, adds a marker on the found location.

    For all options see
        https://github.com/perliedman/leaflet-control-geocoder

    """

    _template = Template(
        """
        {% macro script(this, kwargs) %}
            options = {{ this.options|tojson }}
            options.geocoder = L.Control.Geocoder.google({"apiKey": options.apiKey})
            L.Control.geocoder(
                options
            ).on('markgeocode', function(e) {
                {{ this._parent.get_name() }}.setView(e.geocode.center, 11);
            }).addTo({{ this._parent.get_name() }});

        {% endmacro %}
    """
    )

    def __init__(self, collapsed=False, position="topright", add_marker=True, **kwargs):
        """Init the class."""
        super(GGeocoder, self).__init__()
        self._name = "Geocoder"
        self.options = parse_options(
            collapsed=collapsed,
            position=position,
            defaultMarkGeocode=add_marker,
            apiKey=o_settings.GOOGLE_API_KEY,
            **kwargs,
        )

Many thanks for your help!

0reactions
AusiasMarchcommented, May 20, 2021

I’ll try that. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Places Search Box | Maps JavaScript API - Google Developers
The search box returns a pick list that includes both places and predicted search terms.
Read more >
How to add a Place Autocomplete search widget to your website
In this episode of Geocasts, learn how to add a Place Autocomplete search bar to a webpage with the client-side Maps JavaScript API....
Read more >
Tutorial: Autocomplete Cities Using the Google Places API
How to use the Google Places API with your input fields to autocomplete cities and states. Full code on github: https://git.io/v9ZSt.
Read more >
Search locations on Google Maps - Android
You can search for places and locations with Google Maps. When you sign in to Google Maps, you can get more detailed search...
Read more >
Places Search Bar | PlacesSearchBar - GitHub Pages
An extension of Xamarin.Forms SearchBar control for implementing Google Places Autocomplete API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found