├── LICENSE ├── README.md └── leaflet-geoip.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jakub Dostal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Leaflet.GeoIP 2 | ============= 3 | 4 | GeoIP Plugin for Leaflet.js. This plugin allows you to find the approximate position of the client machine based on their IP address. Additionally, you can center the map on this location. 5 | 6 | 7 | Usage 8 | ============= 9 | Include the .js file in your html file. 10 | ``` 11 | 12 | ``` 13 | Get the approximate position of the client machine as a L.LatLng object: 14 | ``` 15 | var pos = L.GeoIP.getPosition(); 16 | ``` 17 | Centre the map on the approximate location of the client machine (**note:** the `centerMapOnPosition` function calls `getPosition`): 18 | ``` 19 | //initialise your map into 20 | //var map; 21 | 22 | L.GeoIP.centerMapOnPosition(map); 23 | ``` 24 | You have the possibility to pass a zoom parameter as well: 25 | ```javascript 26 | L.GeoIP.centerMapOnPosition(map, 15); 27 | ``` 28 | You can also do the same for the location of an arbitraty IP address: 29 | ``` 30 | var ip = "13.15.13.15"; 31 | var zoom = 15; 32 | var pos = L.GeoIP.getPosition(ip); 33 | 34 | //or 35 | 36 | //initialise your map into 37 | //var map; 38 | L.GeoIP.centerMapOnPosition(map, zoom, ip); 39 | ``` 40 | 41 | Credits 42 | ============= 43 | * [freegeoip.net](http://freegeoip.net) for the free geoIP server and service ([GitHub project](https://github.com/fiorix/freegeoip)) 44 | -------------------------------------------------------------------------------- /leaflet-geoip.js: -------------------------------------------------------------------------------- 1 | L.GeoIP = L.extend({ 2 | 3 | getPosition: function (ip) { 4 | var url = "https://freegeoip.net/json/"; 5 | var result = L.latLng(0, 0); 6 | 7 | if (ip !== undefined) { 8 | url = url + ip; 9 | } else { 10 | //lookup our own ip address 11 | } 12 | 13 | var xhr = new XMLHttpRequest(); 14 | xhr.open("GET", url, false); 15 | xhr.onload = function () { 16 | var status = xhr.status; 17 | if (status == 200) { 18 | var geoip_response = JSON.parse(xhr.responseText); 19 | result.lat = geoip_response.latitude; 20 | result.lng = geoip_response.longitude; 21 | } else { 22 | console.log("Leaflet.GeoIP.getPosition failed because its XMLHttpRequest got this response: " + xhr.status); 23 | } 24 | }; 25 | xhr.send(); 26 | return result; 27 | }, 28 | 29 | centerMapOnPosition: function (map, zoom, ip) { 30 | var position = L.GeoIP.getPosition(ip); 31 | map.setView(position, zoom); 32 | } 33 | }); 34 | --------------------------------------------------------------------------------