1

I have a Leaflet web map generated with qgis2web plugin in QGIS.

By default, there is one option in the plugin generating window to add an address search bar on the map. Unfortunately, that address bar uses OpenStreetMap data, which is not very accurate. Now, I want to change some part of code and place a Google Maps search instead.

Is that possible to do?

1

Export your map with the default OpenStreetMap geocoder. Then edit the exported index.html, and find the following code:

var osmGeocoder = new L.Control.Geocoder({
    collapsed: true,
    position: 'topleft',
    text: 'Search',
    title: 'Testing'
}).addTo(map);

Replace it with this:

var googleGeocoder = new L.Control.Geocoder({
    geocoder: L.Control.Geocoder.google('YOUR_API_KEY', {}),
    collapsed: true,
    position: 'topleft',
    text: 'Search',
    title: 'Testing'
}).addTo(map);

If you need to send other options in your Google query, place them in between the empty braces: {}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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