Created
August 25, 2015 14:40
-
-
Save jonathanread/fd2ddd38bc66f3899f85 to your computer and use it in GitHub Desktop.
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
var directionsService = new google.maps.DirectionsService(); | |
var geocoder = new google.maps.Geocoder(); | |
var infoWindow = new google.maps.InfoWindow(); | |
var map, userLoc, directionsDisplay, locationRequest; | |
$(document).ready(function () { | |
LoadMap(); | |
GetUsersLocation(); | |
locationRequest = getQueryString()["location"]; | |
$(window).scroll(moveMap); | |
}); | |
function moveMap() { | |
var w_Top = $(window).scrollTop(); | |
var containerHeight = $(".mainContent-wrapper").height(); | |
var mapHeight = $("#map_canvas").height(); | |
var maxTopPos = (containerHeight - mapHeight) + 180; | |
if (w_Top > 180) { | |
$("#map_canvas").css({ | |
"position": "fixed", | |
"top": "10px" | |
}); | |
var mapPosTop = $("#map_canvas").offset().top; | |
if (mapPosTop > maxTopPos) { | |
$("#map_canvas").css({ | |
"position": "relative", | |
"top": maxTopPos - 280 + "px" | |
}); | |
} | |
} | |
else | |
{ | |
$("#map_canvas").css({ | |
"position": "relative", | |
"top": "0px" | |
}); | |
} | |
} | |
function LoadMap() { | |
var mapOptions = { | |
center: new google.maps.LatLng(44.98, -93.2636111), | |
zoom: 10, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
map = new google.maps.Map(document.getElementById("map_canvas"), | |
mapOptions); | |
$(".locationInfo").each(function (i) { | |
var a = ""; | |
$(this).children(".address").each(function () { | |
if (a.length > 0) { | |
a += ","; | |
} | |
a += $(this).html().replace(/\s/g, "+"); | |
}); | |
var phone = $(this).children(".ph").html(); | |
var hours = $(this).siblings(".locationHours").html(); | |
GetLocationsLatLong(a, $(this),phone,hours); | |
}); | |
} | |
function GetLocationsLatLong(address, loc, phone, hours) { | |
var addressPretty = address.replace(/\+/g, " ").replace(",", ", "); | |
var city = addressPretty.slice(addressPretty.indexOf(",") + 1, addressPretty.lastIndexOf(",")).trim().replace(/\s/g, ""); | |
if (geocoder) | |
{ | |
geocoder.geocode({ 'address': address }, function (results,status) { | |
if (status == google.maps.GeocoderStatus.OK) | |
{ | |
var l =results[0].geometry.location; | |
latLong = new google.maps.LatLng(l.lat(), l.lng()); | |
SetGeoMarker(l.lat(), l.lng(), address, phone, hours); | |
$(loc).children(".directionsBtn").html("<a class='redLinkButton pie' href='#" + city + "' onclick='GetDirections(" + l.lat() + "," + l.lng() + ",\"" + city + "\")'>Get Directions</a>"); | |
} | |
else | |
{ | |
HandleGeoError(status); | |
} | |
}); | |
} | |
} | |
function SetGeoMarker(lat, lng, address, phone, hours) { | |
var addressPretty = address.replace(/\+/g, " ").replace(",", ", "); | |
var city = addressPretty.slice(addressPretty.indexOf(",") + 1, addressPretty.lastIndexOf(",")).trim().replace(/\s/g,""); | |
var marker = new google.maps.Marker({ | |
position: latLong, | |
icon: "/Sitefinity/WebsiteTemplates/Main/App_Themes/Main/Images/mapsPin.png", | |
title: addressPretty | |
}); | |
google.maps.event.addListener(marker, "click", function () { | |
infoWindow.close(); | |
infoWindow.setContent("<div class='mapsInfo'><div class='info'><h2 style='margin:0;'>Jerry's Hardware</h2>" + | |
address.replace(/\+/g, " ").replace(",", "<br/>") + | |
"<br/>" + phone + "<br />" + | |
"<a class='infoWindowDirections' href='#" + city + "' onclick='GetDirections(" + lat + "," + lng + ",\""+ city +"\")'>Get Directions</a></div>" + | |
"<div class='infoHours'>" + hours + "</div>" + | |
"</div>"); | |
infoWindow.open(map, marker); | |
}); | |
if (locationRequest !== undefined) { | |
if (addressPretty.toLowerCase().indexOf(locationRequest.toLowerCase()) > 0) { | |
google.maps.event.trigger(marker, "click"); | |
} | |
} | |
marker.setMap(map); | |
} | |
function GetDirections(lat, lng, city) { | |
if (directionsDisplay) { | |
directionsDisplay.setMap(null); | |
} | |
directionsDisplay = new google.maps.DirectionsRenderer(); | |
directionsDisplay.setMap(map); | |
var end = new google.maps.LatLng(lat, lng); | |
if (userLoc === undefined) { | |
GetUsersLocation(); | |
if (userLoc === undefined) { | |
var address = prompt("Please enter your current location."); | |
if (address == null) | |
{ | |
return; | |
} | |
if (geocoder) { | |
geocoder.geocode({ 'address': address }, function (results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
var l = results[0].geometry.location; | |
userLoc = new google.maps.LatLng(l.lat(), l.lng()); | |
GetDirections(lat, lng, city); | |
} | |
else | |
{ | |
HandleGeoError(status); | |
} | |
}); | |
} | |
else | |
{ | |
HandleGeoError(status); | |
} | |
} | |
} else { | |
DisplayDirections(end,city); | |
} | |
return false; | |
} | |
function DisplayDirections(end, city) { | |
var request = { | |
origin: userLoc, | |
destination: end, | |
travelMode: google.maps.TravelMode.DRIVING | |
}; | |
directionsService.route(request, function (result, status) { | |
if (status == google.maps.DirectionsStatus.OK) { | |
directionsDisplay.setDirections(result); | |
var route = result.routes[0]; | |
var instructions = "<a id='clear' class='infoWindowDirections' onclick='Clear(\"" + city + "\")'>Clear Map & Location</a><h3>Directions</h3>"; | |
for (var i = 0; i < route.legs.length; i++) { | |
instructions += "<div class='total'>" + route.legs[i].distance.text + " " + route.legs[i].duration.text + "</div>"; | |
for (var j = 0; j < route.legs[i].steps.length; j++) { | |
instructions += "<div>" + (parseInt(j) + 1) + ".) " + route.legs[i].steps[j].instructions + "</div>" + | |
"<div style='margin:0 0 10px 20px;'>" + route.legs[i].steps[j].distance.text + " ( " + route.legs[i].steps[j].duration.text + " )</div>"; | |
} | |
} | |
$("#" + city + " .directions").html(instructions); | |
infoWindow.close(); | |
} | |
else { | |
HandleDirectionsError(status); | |
} | |
}); | |
} | |
function GetUsersLocation() { | |
if (navigator.geolocation && userLoc === undefined) { | |
navigator.geolocation.getCurrentPosition(function (pos) { | |
userLoc = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); | |
}); | |
} | |
else { | |
$("#results").html("Geo Location Not Supported"); | |
} | |
} | |
function Clear(city) { | |
if (directionsDisplay) { | |
directionsDisplay.setMap(null); | |
} | |
LoadMap(); | |
$("#" + city +" .directions").html(""); | |
var usecurrent = confirm("Use your current location?"); | |
if (usecurrent) { | |
GetUsersLocation(); | |
} else { | |
userLoc = undefined; | |
} | |
return false; | |
} | |
function HandleDirectionsError(status) { | |
switch (status) { | |
case google.maps.DirectionsStatus.NOT_FOUND: | |
alert("Your location could not be confirmed. Please try your request again."); | |
Clear(); | |
break; | |
case google.maps.DirectionsStatus.UNKNOWN_ERROR: | |
alert("There was an unknown error while trying to retrieve your directions, please try your request again."); | |
Clear(); | |
break; | |
case google.maps.DirectionsStatus.ZERO_RESULTS: | |
alert("A route could not be determined between your location and Jerry's Hardware."); | |
Clear() | |
break; | |
default: | |
alert("An Error has occurred."); | |
Clear(); | |
} | |
} | |
function HandleGeoError(status) { | |
switch (status) { | |
case "INVALID_REQUEST": | |
alert("There was an unknown error while trying to retrieve your location, please try your request again."); | |
Clear(); | |
break; | |
case "ZERO_RESULTS": | |
alert("Your location could not be found, please try your request again."); | |
Clear(); | |
break; | |
default: | |
alert("An Error has occurred."); | |
Clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment