Skip to content

Commit 9d4d065

Browse files
authored
Merge pull request #10 from CEG-Codes/jeff_branch
adds documentation corrects and halfway_point documentation
2 parents 8ef1598 + 03642f9 commit 9d4d065

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

_docs/create_map.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ var map = new google.maps.Map(document.getElementById("map"), {<options>});
4545
* A full list of interesting options can be found at the [Google Maps API Reference Page](https://developers.google.com/maps/documentation/javascript/reference#MapOptions)
4646

4747
```js
48-
var map_options = {
49-
center: {lat: -34.397, lng: 150.644},
50-
zoom: 8,
51-
mapTypeId: 'HYBRID',
52-
backgroundColor: teal
53-
}
5448

55-
var map = new google.maps.Map(document.getElementById("map"), {map_options});
49+
function initMap()
50+
{
51+
var map_options = {
52+
center: {lat: -34.397, lng: 150.644},
53+
zoom: 8,
54+
mapTypeId: 'roadmap'
55+
}
5656

57+
var map = new google.maps.Map(document.getElementById("map"), {map_options});
58+
}
5759
```
5860

_docs/create_route.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ function calcRoute() {
5555
```js
5656

5757
var directionsDisplay = new google.maps.DirectionsRenderer();
58+
directionsDisplay.setMap(map);
5859

5960
//Put this line inside the <status == 'OK'> callback function from the code above
60-
61+
6162
directionsDisplay.setDirections(result);
6263
console.log(result);
6364

_docs/find_halfway_point.md

+49
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,54 @@
22
##Finding the Halfway Point || [Back to Table of Contents](_table_of_contents.md)
33

44
####Intro
5+
Using [Google Maps API Reference](https://developers.google.com/maps/documentation/javascript/reference#DirectionsRoute)
6+
7+
Once we have get the route JSON object back from google, we have access to a lot of information including the distance between the two points.
8+
```js
9+
results.routes[0].legs[0].distance
10+
```
11+
12+
The challenge is to show the user a halfway point on the map that is halfway between the origin and destination and along the *route*, not halfway between a straight line. This means that if the route curves around a lake, then the halfway point appears on the *road* and not in the middle of the lake.
13+
14+
Luckily, the route JSON object includes something called the overview_path
15+
16+
```js
17+
results.routes[0].overview_path
18+
```
19+
20+
The overview_path is an array of lat/lng coordinates along the route. We should be able to iterate through the array and find the single lat/lng coordinate the represents the halfway point of the route.
21+
22+
So how do we do this? Good question, Jeff.
23+
24+
One idea is to simply take the overview_path array and divide it by 2 to get the center of the array.
25+
26+
For example:
27+
```js
28+
coordinates_array = results.routes[0].overview_path
29+
half = (coordinates_array.length / 2)
30+
halfway_point = coordinates_array[half]
31+
32+
> halfway_point = {lat: XX, lng: XX}
33+
34+
```
35+
36+
This might work? But there's no way to know if this point is actually halfway between the origin and destination, just that it's in the center of the array that google gave us.
37+
38+
Luckily, we have a method to check the distance of one point to another.
39+
[From Google Reference](https://developers.google.com/maps/documentation/javascript/reference#spherical) use the computeDistanceBetween(LatLng, LatLng) method.
40+
41+
We can calculate the distance from the starting point to the halfway point and also the distance from the ending point to the halfway point. If these numbers are about equal, then we have a good halfway point.
42+
```js
43+
//Distance from starting point to halfway_point
44+
45+
computeDistanceBetween(starting_point, halfway_point)
46+
47+
48+
//Distance from ending point to halfway_point
49+
50+
computeDistanceBetween(ending_point, halfway_point)
51+
52+
//If these two numbers are about equal, then we have a good halfway point.
53+
```
554

655
####Coding

0 commit comments

Comments
 (0)