Geocoding Leaflet plugin

Leaflet geocoding controls

Geocoding tool

Simple Leaflet geocoding control, comes with an default set of providers:

  • OpenStreetMap
  • Google
  • Bing
  • Esri
  • Geonames
  • Nokia
  • Yandex
  • RuCadastre

Add script

<script src="http://olegsmith.github.com/leaflet.geocoding/leaflet.geocoding.js"></script>

<script>
    var map = L.map('map').setView([55.751667, 37.617778], 13);
    L.control.scale().addTo(map);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
         maxZoom: 18
        , attribution: '© OpenStreetMap contributors'
        }).addTo(map);

    var geocoding = new L.Geocoding({
      apikeys : {
        'bing' : 'bingKey'
        , 'nokia' : 'nokiaKey'
      }
    });

    map.addControl(geocoding);
  

</script>

Geocode

<script>

      geocoding.geocode('address');

</script>
Name type default description
provider string 'osm' Current provider
providers object {'osm' : function() {}, 'google' : function() {}, ...} Providers geocode functions
apikeys object {bing: 'bingKey', ...} API keys
apikeys.bing string Bing API key
apikeys.nokia string Nokia API key
Name argument type description
geocode string Geocode
setOptions object Set options

Add script

<script>

      // OSM-like custom geocoding provider
      var custom_geocoding_provider = function(arg) {
        var that = this
            , query = arg.query
            , cb = arg.cb;

        $.ajax({
            url : 'http://nominatim.openstreetmap.org/search'
            , dataType : 'jsonp'
            , jsonp : 'json_callback'
            , data : {
                'q' : query
                , 'format' : 'json'
            }
        })
        .done(function(data){
            if (data.length>0) {
                var res=data[0];
                cb({
                    query : query
                    , content : res.display_name
                    , latlng : new L.LatLng(res.lat, res.lon)
                    , bounds : new L.LatLngBounds([res.boundingbox[0], res.boundingbox[2]], [res.boundingbox[1], res.boundingbox[3]])
                });
            }
        });
      }

      // Add custom provider to provider list
      var geocoding = new L.Geocoding({
        apikeys : {
          'bing' : 'Anqm0F_JjIZvT0P3abS6KONpaBaKuTnITRrnYuiJCE0WOhH6ZbE4DzeT6brvKVR5'
          , 'nokia' : 'HGHGJHgdbndbmbsdf7675wefdvvdsnb'
        }
        , providers : {
          'custom' : custom_geocoding_provider
        }
      });

      // Set custom provider default
      geocoding.setOptions({provider:'custom'})

      // Geocoding
      geocoding.geocode('address')

</script>