Last active
December 20, 2015 18:29
-
-
Save nola/6176250 to your computer and use it in GitHub Desktop.
Google maps html5 geolocation
This file contains 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
<script> | |
$(function(){ | |
var startingLocation; | |
var destination1544 = new google.maps.LatLng(30.439112, -84.257061); | |
var destination2020 = new google.maps.LatLng(30.440659, -84.318076); | |
navigator.geolocation.getCurrentPosition(function (position){ | |
//lat and long | |
var latitude = position.coords.latitude; | |
var longitude = position.coords.longitude; | |
startingLocation = latitude + "," + longitude; | |
}); | |
function goToGoogleMaps(s, d){ | |
window.open("https://maps.google.com/maps?saddr=" + s + "&daddr=" + d, '_blank'); | |
} | |
$(".location-1544").click(function(e){ | |
//check for geo | |
if (navigator.geolocation){ | |
goToGoogleMaps(startingLocation, destination1544); | |
} else { | |
window.open("https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=1544-2+Governors+Square+Blvd,+Tallahassee,+FL&aq=0&oq=1544-2+Governors+Square+Blvd.&sll=30.43405,-87.249161&sspn=0.008797,0.011973&vpsrc=0&ie=UTF8&hq=&hnear=1544+Governors+Square+Blvd,+Tallahassee,+Florida+32301&t=m&z=17&iwloc=A", '_blank'); | |
} | |
e.preventDefault(); | |
}); | |
$(".location-2020").click(function(e){ | |
//check for geo | |
if (navigator.geolocation){ | |
goToGoogleMaps(startingLocation, destination2020); | |
} else { | |
window.open("https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=2020+W.+Pensacola+Street,+Suite+210&aq=&sll=30.032996,-89.882563&sspn=0.565324,0.766296&vpsrc=0&ie=UTF8&hq=&hnear=2020+N+W+St+%23210,+Pensacola,+Florida+32505&t=m&z=17&iwloc=A", '_blank'); | |
} | |
//go to Google Maps Function - takes a starting location and destination and sends the query to google maps | |
e.preventDefault(); | |
}); | |
}); | |
</script> |
$(function(){
var startingLocation;
var destination1544 = new google.maps.LatLng(30.439112, -84.257061);
var destination2020 = new google.maps.LatLng(30.440659, -84.318076);
$('.location-1544').click(function (e) {
e.preventDefault();
// check if browser supports geolocation
if (navigator.geolocation) {
// get user's current position
navigator.geolocation.getCurrentPosition(function (position) {
// get latitude and longitude
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
startingLocation = latitude + "," + longitude;
// send starting location and destination to goToGoogleMaps function
goToGoogleMaps(startingLocation, destination1544);
});
}
// fallback for browsers without geolocation
else {
}
// go to Google Maps function - takes a starting location and destination and sends the query to Google Maps
function goToGoogleMaps(startingLocation, destination) {
window.location = "https://maps.google.com/maps?saddr=" + startingLocation + "&daddr=" + destination;
}
});
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.dconnell.co.uk/blog/index.php/2013/05/17/create-a-give-me-directions-button-using-jquery-and-html5-geolocation/#comment-268