Skip to content

Commit

Permalink
SM only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Dolgov committed Mar 31, 2017
1 parent 8108ccd commit 38d13c7
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"repository": {
"type": "git",
"url": "git://github.com/perliedman/leaflet-control-geocoder.git"
"url": "git://github.com/sputnik-maps/leaflet-control-geocoder.git"
},
"keywords": [
"leaflet",
Expand Down
43 changes: 43 additions & 0 deletions src/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var L = require('leaflet'),
Control = require('./control'),
Sputnik = require('./geocoders/sputnik'),
Nominatim = require('./geocoders/nominatim'),
Bing = require('./geocoders/bing'),
MapQuest = require('./geocoders/mapquest'),
Mapbox = require('./geocoders/mapbox'),
What3Words = require('./geocoders/what3words'),
Google = require('./geocoders/google'),
Photon = require('./geocoders/photon'),
Mapzen = require('./geocoders/mapzen'),
ArcGis = require('./geocoders/arcgis'),
HERE = require('./geocoders/here');

module.exports = L.Util.extend(Control.class, {
Sputnik: Sputnik.class,
sputnik: Sputnik.factory,
Nominatim: Nominatim.class,
nominatim: Nominatim.factory,
Bing: Bing.class,
bing: Bing.factory,
MapQuest: MapQuest.class,
mapQuest: MapQuest.factory,
Mapbox: Mapbox.class,
mapbox: Mapbox.factory,
What3Words: What3Words.class,
what3words: What3Words.factory,
Google: Google.class,
google: Google.factory,
Photon: Photon.class,
photon: Photon.factory,
Mapzen: Mapzen.class,
mapzen: Mapzen.factory,
ArcGis: ArcGis.class,
arcgis: ArcGis.factory,
HERE: HERE.class,
here: HERE.factory
});

L.Util.extend(L.Control, {
Geocoder: module.exports,
geocoder: Control.factory
});
10 changes: 5 additions & 5 deletions src/control.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var L = require('leaflet'),
Nominatim = require('./geocoders/nominatim').class;
Sputnik = require('./geocoders/sputnik').class;

module.exports = {
class: L.Control.extend({
Expand All @@ -8,8 +8,8 @@ module.exports = {
collapsed: true,
expand: 'click',
position: 'topright',
placeholder: 'Search...',
errorMessage: 'Nothing found.',
placeholder: 'Поиск...',
errorMessage: 'Не найдено',
suggestMinLength: 3,
suggestTimeout: 250,
defaultMarkGeocode: true
Expand All @@ -20,7 +20,7 @@ module.exports = {
initialize: function (options) {
L.Util.setOptions(this, options);
if (!this.options.geocoder) {
this.options.geocoder = new Nominatim();
this.options.geocoder = new Sputnik();
}

this._requestCount = 0;
Expand Down Expand Up @@ -120,7 +120,7 @@ module.exports = {
this._map.removeLayer(this._geocodeMarker);
}

this._geocodeMarker = new L.Marker(result.center)
this._geocodeMarker = new (L.sm.Marker || L.Marker)(result.center)
.bindPopup(result.html || result.name)
.addTo(this._map)
.openPopup();
Expand Down
92 changes: 92 additions & 0 deletions src/geocoders/sputnik.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
var L = require('leaflet'),
Util = require('../util');

module.exports = {
class: L.Class.extend({
options: {
markerClass: Util.extract(L, 'sm.Marker', L.Marker),
geocoderUrl: 'http://search.maps.sputnik.ru/search/addr',
reverseGeocoderUrl: 'http://whatsthere.maps.sputnik.ru/point'
},

_bboxDeltas: {
"country": {
"latD": 29.4912965,
"lngD": 122.871094
},
"region": {
"latD": 2.2116335,
"lngD": 7.6794435
},
"district": {
"latD": 0.2752005,
"lngD": 0.9599305
},
"place": {
"latD": 0.1496885,
"lngD": 0.4837415
},
"street": {
"latD": 0.004543,
"lngD": 0.015117
},
"house": {
"latD": 0.0011355,
"lngD": 0.0037795
},
"default": {
"latD": 0.1,
"lngD": 0.1
}
},

getBBox: function ( center, type ) {
type = type in this._bboxDeltas ? type : 'default';
var deltas = this._bboxDeltas[type];
return L.latLngBounds(
L.latLng(center.lat - deltas.latD, center.lng - deltas.lngD ),
L.latLng(center.lat + deltas.latD, center.lng + deltas.lngD )
)
},

geocode: function (query, cb, context) {
Util.jsonp(this.options.geocoderUrl, {
q: query
}, function(data) {
cb.call(context, this._prepareResult(data));
}, this, 'callback');
},

reverse: function(location, scale, cb, context) {
Util.jsonp(this.options.reverseGeocoderUrl , {
lat: location.lat,
lon: location.lng,
houses : true
}, function(data) {
cb.call(context, this._prepareResult(data));
}, this, 'callback');
},
_prepareResult: function (data) {
var _this = this;
var features = [].concat(Util.extract(data, 'result.address.0.features', []), Util.extract(data, 'result.address.1.features', []));
return features.map(function(feature){
var feats = L.geoJson(feature);
var bounds = feats && feats.getBounds(),
center = bounds.getCenter();
var bbox = _this.getBBox(center, feature.properties.type);
var name = [feature.properties.title, feature.properties.description]
.filter(function(value){ return !! value; })
.join(', ');
return {
name: name,
bbox: bbox,
center: bounds.getCenter()
}
});
}
}),

factory: function(options) {
return new L.Control.Geocoder.Sputnik(options);
}
};
33 changes: 3 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
var L = require('leaflet'),
Control = require('./control'),
Nominatim = require('./geocoders/nominatim'),
Bing = require('./geocoders/bing'),
MapQuest = require('./geocoders/mapquest'),
Mapbox = require('./geocoders/mapbox'),
What3Words = require('./geocoders/what3words'),
Google = require('./geocoders/google'),
Photon = require('./geocoders/photon'),
Mapzen = require('./geocoders/mapzen'),
ArcGis = require('./geocoders/arcgis'),
HERE = require('./geocoders/here');
Sputnik = require('./geocoders/sputnik');

module.exports = L.Util.extend(Control.class, {
Nominatim: Nominatim.class,
nominatim: Nominatim.factory,
Bing: Bing.class,
bing: Bing.factory,
MapQuest: MapQuest.class,
mapQuest: MapQuest.factory,
Mapbox: Mapbox.class,
mapbox: Mapbox.factory,
What3Words: What3Words.class,
what3words: What3Words.factory,
Google: Google.class,
google: Google.factory,
Photon: Photon.class,
photon: Photon.factory,
Mapzen: Mapzen.class,
mapzen: Mapzen.factory,
ArcGis: ArcGis.class,
arcgis: ArcGis.factory,
HERE: HERE.class,
here: HERE.factory
Sputnik: Sputnik.class,
sputnik: Sputnik.factory
});

L.Util.extend(L.Control, {
Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ var L = require('leaflet'),
})();

module.exports = {
extract: function extract(root, path, def){
var key,
val = !!root ? root : this,
arr = String(path).replace(/'|"|\]/g,'').replace(/\[/g,'.').split('.');
while ((key = arr.shift()) && 'object' == typeof val && val) {
val = 'undefined' == typeof val[key]? ('undefined' == typeof def? false : def) : val[key];
}
return val;
},

jsonp: function(url, params, callback, context, jsonpParam) {
var callbackId = '_l_geocoder_' + (lastCallbackId++);
params[jsonpParam || 'callback'] = callbackId;
Expand Down
2 changes: 1 addition & 1 deletion test/browserify.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>Leaflet Control Geocoder</title>

<meta name='viewport' content='width=device-width, user-scalable=no initial-scale=1, maximum-scale=1'>
<meta name='viewport' content='width=device-width, user-scalable=no initial-scale=1, maximum-scale=1'>

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<link rel="stylesheet" href="../dist/Control.Geocoder.css" />
Expand Down
2 changes: 1 addition & 1 deletion test/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var L = require('leaflet');
require('../dist/Control.Geocoder');

var map = L.map('map').setView([0, 0], 2),
geocoder = L.Control.Geocoder.mapzen('search-DopSHJw'),
geocoder = L.Control.Geocoder.sputnik(),
control = L.Control.geocoder({
geocoder: geocoder
}).addTo(map),
Expand Down
4 changes: 2 additions & 2 deletions test/position.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<head>
<title>Leaflet Control Geocoder</title>

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<link rel="stylesheet" href="../Control.Geocoder.css" />

<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet-src.js"></script>
<script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
<script src="../Control.Geocoder.js"></script>
<style type="text/css">
body {
Expand Down
4 changes: 2 additions & 2 deletions test/touch.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Leaflet Control Geocoder</title>

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<link rel="stylesheet" href="../Control.Geocoder.css" />

<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet-src.js"></script>
<script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
<script src="../Control.Geocoder.js"></script>
<style type="text/css">
body {
Expand Down

0 comments on commit 38d13c7

Please sign in to comment.