perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

How to use own Geocoder Service instead of L.Control.Geocoder.nominatim(). #674

Closed dev1ninja closed 1 year ago

dev1ninja commented 1 year ago

Hi there, I'm running my own Geocoder Service(I installed based on installation guide) It is Geo.mygovernmentonline.org. And here is my code.

L.Control.geocoder().addTo(map);
var routeControl = L.Routing.control({
    waypoints: [],
    serviceUrl: 'https://osrm.mgoconnect.org/route/v1',
    geocoder: L.Control.Geocoder.nominatim(),
    routeWhileDragging: true,
}).addTo(map);

var geocoder = L.Control.Geocoder.nominatim();

geocoder.geocode(address[i][0], function (results) {
      if (results[0] !== undefined) {
          resolve(L.latLng(results[0].properties.lat, results[0].properties.lon));
      } else {
          resolve()
      }
  })

As I mentioned in Title, now I would like to use my own Geocoder Service instead of geocoder: L.Control.Geocoder.nominatim(), var geocoder = L.Control.Geocoder.nominatim()

Please help me. Thank you!

curtisy1 commented 1 year ago

Hey there,

you have two options for this one which both more or less do the same thing.

  1. You can implement your own geocoder, taking the nominatim one as a guide

  2. extend the nominatim one via

    class YourGeocoder extends L.Control.Geocoder.Nominatim {
    // override the geocode method here
    }
  3. depending on what your geocoding server does, you might also use an existing geocoder and replace the serviceUrl

    const geocoder = L.Control.Geocoder.nominatim({ serviceUrl: "Geo.mygovernmentonline.org" });
dev1ninja commented 1 year ago

I understand. This is very helpful. Thank you!