PageSourceSearch

https://www.thoitietvietnam.gov.vn/kttv/leaflet/L.Control.OSMGeocoder.js

js thoitietvietnam.gov.vn collected 2026-10-03 18:05:10 UTC 5,490 bytes, 195 lines download raw bytes

1if (typeof console == "undefined") {
2	this.console = { log: function (msg) { /* do nothing since it would otherwise break IE */} };
3}
4
5
6L.Control.OSMGeocoder = L.Control.extend({
7	options: {
8		collapsed: true,
9		position: 'bottomright',
10		text: 'Tìm',
11		placeholder: '',
12		bounds: null, // L.LatLngBounds
13		email: null, // String
14		callback: function (results) {
15			if (results.length == 0) {
16				console.log("Không tìm thấy");
17				return;
18			}
19			var bbox = results[0].boundingbox,
20				first = new L.LatLng(bbox[0], bbox[2]),
21				second = new L.LatLng(bbox[1], bbox[3]),
22				bounds = new L.LatLngBounds([first, second]);
23			this._map.fitBounds(bounds);
24		}
25	},
26
27	_callbackId: 0,
28
29	initialize: function (options) {
30		L.Util.setOptions(this, options);
31	},
32
33	onAdd: function (map) {
34		this._map = map;
35
36		var className = 'leaflet-control-geocoder',
37			container = this._container = L.DomUtil.create('div', className);
38
39		L.DomEvent.disableClickPropagation(container);
40
41		var form = this._form = L.DomUtil.create('form', className + '-form');
42
43		var input = this._input = document.createElement('input');
44		input.type = "text";
45		input.placeholder = this.options.placeholder || '';
46
47		var submit = document.createElement('input');
48		submit.type = "submit";
49		submit.value = this.options.text;
50
51		form.appendChild(input);
52		form.appendChild(submit);
53
54		L.DomEvent.addListener(form, 'submit', this._geocode, this);
55
56		if (this.options.collapsed) {
57			L.DomEvent.addListener(container, 'mouseover', this._expand, this);
58			L.DomEvent.addListener(container, 'mouseout', this._collapse, this);
59
60			var link = this._layersLink = L.DomUtil.create('a', className + '-toggle', container);
61			link.href = '#';
62			link.title = 'Nominatim Geocoder';
63
64			L.DomEvent.addListener(link, L.Browser.touch ? 'click' : 'focus', this._expand, this);
65
66			this._map.on('movestart', this._collapse, this);
67		} else {
68			this._expand();
69		}
70
71		container.appendChild(form);
72
73		return container;
74	},
75
76	/* helper functions for cordinate extraction */
77	_createSearchResult : function(lat, lon) {
78		//creates an position description similar to the result of a Nominatim search
79		var diff = 0.005;
80		var result = [];
81		result[0] = {};
82		result[0]["boundingbox"] = [parseFloat(lat)-diff,parseFloat(lat)+diff,parseFloat(lon)-diff,parseFloat(lon)+diff];
83		result[0]["class"]="boundary";
84		result[0]["display_name"]="Position: "+lat+" "+lon;
85		result[0]["lat"] = lat;
86		result[0]["lon"] = lon;
87		return result;
88	},
89	_isLatLon : function (q) {
90		//"lon lat" => xx.xxx x.xxxxx
91		var re = /(-?\d+\.\d+)\s(-?\d+\.\d+)/;
92		var m = re.exec(q);
93		if (m != undefined) return m;
94
95		//lat...xx.xxx...lon...x.xxxxx
96		re = /lat\D*(-?\d+\.\d+)\D*lon\D*(-?\d+\.\d+)/;
97		m = re.exec(q);
98		//showRegExpResult(m);
99		if (m != undefined) return m;
100		else return null;
101	},
102	_isLatLon_decMin : function (q) {
103		console.log("is LatLon?: "+q);
104		//N 53° 13.785' E 010° 23.887'
105		//re = /[NS]\s*(\d+)\D*(\d+\.\d+).?\s*[EW]\s*(\d+)\D*(\d+\.\d+)\D*/;
106		re = /([ns])\s*(\d+)\D*(\d+\.\d+).?\s*([ew])\s*(\d+)\D*(\d+\.\d+)/i;
107		m = re.exec(q.toLowerCase());
108		//showRegExpResult(m);
109		if ((m != undefined)) return m;
110		else return null;
111		// +- dec min +- dec min
112	},
113
114	_geocode : function (event) {
115		L.DomEvent.preventDefault(event);
116		var q = this._input.value;
117		//try to find corrdinates
118		if (this._isLatLon(q) != null)
119		{
120			var m = this._isLatLon(q);
121			console.log("LatLon: "+m[1]+" "+m[2]);
122			//m = {lon, lat}
123			this.options.callback.call(this, this._createSearchResult(m[1],m[2]));
124			return;
125		}
126		else if (this._isLatLon_decMin(q) != null)
127		{
128			var m = this._isLatLon_decMin(q);
129			//m: [ns, lat dec, lat min, ew, lon dec, lon min]
130			var temp  = new Array();
131			temp['n'] = 1;
132			temp['s'] = -1;
133			temp['e'] = 1;
134			temp['w'] = -1;
135			this.options.callback.call(this,this._createSearchResult(
136				temp[m[1]]*(Number(m[2]) + m[3]/60),
137				temp[m[4]]*(Number(m[5]) + m[6]/60)
138			));
139			return;
140		}
141
142		//and now Nominatim
143		//http://wiki.openstreetmap.org/wiki/Nominatim
144		console.log(this._callbackId);
145		window[("_l_osmgeocoder_"+this._callbackId)] = L.Util.bind(this.options.callback, this);
146
147
148		/* Set up params to send to Nominatim */
149		var params = {
150			// Defaults
151			q: this._input.value,
152			json_callback : ("_l_osmgeocoder_"+this._callbackId++),
153			format: 'json'
154		};
155
156		if (this.options.bounds && this.options.bounds != null) {
157			if( this.options.bounds instanceof L.LatLngBounds ) {
158				params.viewbox = this.options.bounds.toBBoxString();
159				params.bounded = 1;
160			}
161			else {
162				console.log('bounds must be of type L.LatLngBounds');
163				return;
164			}
165		}
166
167		if (this.options.email && this.options.email != null) {
168			if (typeof this.options.email == 'string') {
169				params.email = this.options.email;
170			}
171			else{
172				console.log('email must be a string');
173			}
174		}
175
176		var protocol = location.protocol;
177		if (protocol == "file:") protocol = "https:";
178		var url = protocol + "//nominatim.openstreetmap.org/search" + L.Util.getParamString(params),
179			script = document.createElement("script");
180
181
182		script.type = "text/javascript";
183		script.src = url + '&countrycodes=vn';
184		script.id = this._callbackId;
185		document.getElementsByTagName("head")[0].appendChild(script);
186	},
187
188	_expand: function () {
189		L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-expanded');
190	},
191
192	_collapse: function () {
193		this._container.className = this._container.className.replace(' leaflet-control-geocoder-expanded', '');
194	}
195});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.