Skip to content

Instantly share code, notes, and snippets.

@ericnkatz
Created January 16, 2012 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnkatz/1622565 to your computer and use it in GitHub Desktop.
Save ericnkatz/1622565 to your computer and use it in GitHub Desktop.
Using, jquery, visitorjs + google maps for displaying locale information
<script type="text/javascript"src="http://maps.googleapis.com/maps/api/js?key=API&sensor=false">
</script>
<script type="text/javascript" src="http://www.visitorjs.com/visitor.js?key=API"></script>
<script type="text/javascript">
$(document).ready(function (){
Columbus = "Columbus, OH";
Cleveland = "Cleveland, OH";
Dayton = "Dayton, OH";
// Make sure the visitor object is available
if (visitor) {
//
visitorLatitude = visitor.geo.coordinates.latitude;
visitorLongitude = visitor.geo.coordinates.longitude;
visitorOrigin(visitorLatitude, visitorLongitude);
// Use the next line to test locations that should trigger specific content to be displayed.
// visitorOrigin = "Spencer, OH";
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [visitorOrigin],
destinations: [Columbus, Cleveland, Dayton],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL
}, callback);
}
});
var distanceTo = new Array();
function visitorOrigin(lat, lng) {
visitorOrigin = new google.maps.LatLng(lat, lng);
}
function callback(response, status) {
//Parsing results
if (status == google.maps.DistanceMatrixStatus.OK) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
//.value shows numeric value of the distance (seems arbitrary without units)
var distance2 = element.distance.value;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
//Used the alert to test output of distances
//alert("Distance to " + to + ": " + distance2);
distanceTo[j] = distance2;
}
}
// 64000 = is roughly estimated at ~ 40 mi of distance,
// so if anyone visits and is not within 40 mi of specific location nothing is displayed
if (distanceTo[0] < distanceTo[1] && distanceTo[0] < distanceTo[2] && distanceTo[0] < 64000){
//Display Columbus Information
$('p#contact').prepend('Your Local Number: <span class="large local">(614) xxx-xxxx </span><br />');
}
else if (distanceTo[1] < distanceTo[0] && distanceTo[1] < distanceTo[2] && distanceTo[1] < 64000) {
//Display Clevelend Information
$('p#contact').prepend('Your Local Number: <span class="large local">(440) xxx-xxxx </span><br />');
}
else if (distanceTo[2] < distanceTo[0] && distanceTo[2] < distanceTo[1] && distanceTo[2] < 64000) {
//Display Dayton Information
$('p#contact').prepend('Your Local Number: <span class="large local">(937) xxx-xxxx </span><br />');
}
else {
//Do nothing.
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment