Skip to content

Instantly share code, notes, and snippets.

@nola
Last active December 20, 2015 18:29
Show Gist options
  • Save nola/6176250 to your computer and use it in GitHub Desktop.
Save nola/6176250 to your computer and use it in GitHub Desktop.
Google maps html5 geolocation
<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>
@nola
Copy link
Author

nola commented Aug 9, 2013

$(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