Skip to content

Commit

Permalink
switched chapters 5 and 6
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Aug 31, 2011
1 parent 923d8c5 commit 0ceff94
Show file tree
Hide file tree
Showing 37 changed files with 1,806 additions and 0 deletions.
20 changes: 20 additions & 0 deletions chapter5/accuracy/myLoc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* myLoc.css
*
*/

body {
font-family: Arial, Helvetica, sans-serif;
margin: 10px;
}
form, div#location, div#distance {
padding: 5px;
}

div#map {
margin: 5px;
width: 400px;
height: 400px;
border: 1px solid black;
}

27 changes: 27 additions & 0 deletions chapter5/accuracy/myLoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html>
<head>
<title>Where am I?</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<meta charset="utf-8">
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="myLoc.js"></script>
<link rel="stylesheet" href="myLoc.css">
</head>
<body>

<div id="location">
Your location will go here.
</div>

<div id="distance">
Distance from WickedlySmart HQ will go here.
</div>

<div id="map">
</div>

</body>
</html>


119 changes: 119 additions & 0 deletions chapter5/accuracy/myLoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* myLoc.js */

var map = null;
var ourCoords = {
latitude: 47.624851,
longitude: -122.52099
};

window.onload = getMyLocation;

function getMyLocation() {
if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(
displayLocation,
displayError);
}
else {
alert("Oops, no geolocation support");
}
}

function displayLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;

var div = document.getElementById("location");
div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
div.innerHTML += " (with " + position.coords.accuracy + " meters accuracy)";

var km = computeDistance(position.coords, ourCoords);
var distance = document.getElementById("distance");
distance.innerHTML = "You are " + km + " km from the WickedlySmart HQ";

showMap(position.coords);
}


// --------------------- Ready Bake ------------------
//
// Uses the Spherical Law of Cosines to find the distance
// between two lat/long points
//
function computeDistance(startCoords, destCoords) {
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);

var Radius = 6371; // radius of the Earth in km
var distance = Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)) * Radius;

return distance;
}

function degreesToRadians(degrees) {
radians = (degrees * Math.PI)/180;
return radians;
}

// ------------------ End Ready Bake -----------------

function showMap(coords) {
var googleLatAndLong = new google.maps.LatLng(coords.latitude,
coords.longitude);
var mapOptions = {
zoom: 10,
center: googleLatAndLong,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var mapDiv = document.getElementById("map");
map = new google.maps.Map(mapDiv, mapOptions);

// add the user marker
var title = "Your Location";
var content = "You are here: " + coords.latitude + ", " + coords.longitude;
addMarker(map, googleLatAndLong, title, content);

}

function addMarker(map, latlong, title, content) {
var markerOptions = {
position: latlong,
map: map,
title: title,
clickable: true
};
var marker = new google.maps.Marker(markerOptions);

var infoWindowOptions = {
content: content,
position: latlong
};

var infoWindow = new google.maps.InfoWindow(infoWindowOptions);

google.maps.event.addListener(marker, 'click', function() {
infoWindow.open(map);
});
}


function displayError(error) {
var errorTypes = {
0: "Unknown error",
1: "Permission denied",
2: "Position is not available",
3: "Request timeout"
};
var errorMessage = errorTypes[error.code];
if (error.code == 0 || error.code == 2) {
errorMessage = errorMessage + " " + error.message;
}
var div = document.getElementById("location");
div.innerHTML = errorMessage;
}

12 changes: 12 additions & 0 deletions chapter5/distance/myLoc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* myLoc.css
*
*/

body {
font-family: Arial, Helvetica, sans-serif;
margin: 10px;
}
div#location, div#distance {
padding: 5px;
}
23 changes: 23 additions & 0 deletions chapter5/distance/myLoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html>
<head>
<title>Where am I?</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<meta charset="utf-8">
<script src="myLoc.js"></script>
<link rel="stylesheet" href="myLoc.css">
</head>
<body>

<div id="location">
Your location will go here.
</div>

<div id="distance">
Distance from WickedlySmart HQ will go here.
</div>

</body>
</html>


77 changes: 77 additions & 0 deletions chapter5/distance/myLoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* myLoc.js */

var ourCoords = {
latitude: 47.624851,
longitude: -122.52099
};

window.onload = getMyLocation;

function getMyLocation() {
if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(
displayLocation,
displayError);
}
else {
alert("Oops, no geolocation support");
}
}

function displayLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;

var div = document.getElementById("location");
div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;

var km = computeDistance(position.coords, ourCoords);
var distance = document.getElementById("distance");
distance.innerHTML = "You are " + km + " km from the WickedlySmart HQ";

}


// --------------------- Ready Bake ------------------
//
// Uses the Spherical Law of Cosines to find the distance
// between two lat/long points
//
function computeDistance(startCoords, destCoords) {
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);

var Radius = 6371; // radius of the Earth in km
var distance = Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)) * Radius;

return distance;
}

function degreesToRadians(degrees) {
radians = (degrees * Math.PI)/180;
return radians;
}

// ------------------ End Ready Bake -----------------


function displayError(error) {
var errorTypes = {
0: "Unknown error",
1: "Permission denied",
2: "Position is not available",
3: "Request timeout"
};
var errorMessage = errorTypes[error.code];
if (error.code == 0 || error.code == 2) {
errorMessage = errorMessage + " " + error.message;
}
var div = document.getElementById("location");
div.innerHTML = errorMessage;
}

20 changes: 20 additions & 0 deletions chapter5/final/myLoc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* myLoc.css
*
*/

body {
font-family: Arial, Helvetica, sans-serif;
margin: 10px;
}
form, div#location, div#distance {
padding: 5px;
}

div#map {
margin: 5px;
width: 400px;
height: 400px;
border: 1px solid black;
}

32 changes: 32 additions & 0 deletions chapter5/final/myLoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html>
<head>
<title>Wherever you go, there you are</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<meta charset="utf-8">
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="myLoc.js"></script>
<link rel="stylesheet" href="myLoc.css">
</head>
<body>

<form>
<input type="button" id="watch" value="Watch me">
<input type="button" id="clearWatch" value="Clear watch">
</form>

<div id="location">
Your location will go here.
</div>

<div id="distance">
Distance from WickedlySmart HQ will go here.
</div>

<div id="map">
</div>

</body>
</html>


Loading

0 comments on commit 0ceff94

Please sign in to comment.