Last active
October 25, 2017 02:46
-
-
Save wboykinm/7326973 to your computer and use it in GitHub Desktop.
Mapbox Geolocation by IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title></title> | |
<script src='http://api.tiles.mapbox.com/mapbox.js/v1.4.0/mapbox.js'></script> | |
<script src='http://codeorigin.jquery.com/jquery-1.10.2.min.js'></script> | |
<link href='http://api.tiles.mapbox.com/mapbox.js/v1.4.0/mapbox.css' rel='stylesheet' /> | |
<!--[if lte IE 8]> | |
<link href='http://api.tiles.mapbox.com/mapbox.js/v1.4.0/mapbox.ie.css' rel='stylesheet'> | |
<![endif]--> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script type='text/javascript'> | |
// Set default location in case the IP doesn't have one | |
var lat = 40.8; | |
var lon = -96.67; | |
// Grab IP location from freegeoip API | |
$.getJSON('http://freegeoip.net/json/', function(json) { | |
if (json) { | |
lat = json.latitude; | |
lon = json.longitude; | |
} | |
// Build the map | |
var map = L.mapbox.map('map', 'landplanner.g7443phg').setView([lat, lon], 8); | |
// Add a draggable marker at the IP location | |
var marker = L.marker(new L.LatLng(lat, lon), { | |
icon: L.icon({ | |
iconUrl: "http://farm6.staticflickr.com/5519/10708548293_e38037720a_o.png", | |
iconSize: [128, 128], | |
iconAnchor: [64, 64], | |
popupAnchor: [0, -64], | |
className: "dot" | |
}), | |
draggable: true | |
}); | |
marker.bindPopup('Am I off the map? Drag me to the right spot!'); | |
marker.addTo(map); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment