-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-geoip.is
33 lines (29 loc) · 996 Bytes
/
leaflet-geoip.is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
L.GeoIP = L.extend({
getPosition: function (ip) {
var url = "http://freegeoip.net/json/";
var result = L.latLng(0, 0);
if (ip !== undefined) {
url = url + ip;
} else {
//lookup our own ip address
}
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.onload = function () {
var status = xhr.status;
if (status == 200) {
var geoip_response = JSON.parse(xhr.responseText);
result.lat = geoip_response.latitude;
result.lng = geoip_response.longitude;
} else {
console.log("Leaflet.GeoIP.getPosition failed because its XMLHttpRequest got this response: " + xhr.status);
}
};
xhr.send();
return result;
},
centerMapOnPosition: function (map, zoom, ip) {
var position = L.GeoIP.getPosition(ip);
map.setView(position, zoom);
}
});