March 29, 2015
Hot Topics:

Measuring a Running Route

As an avid cyclist, one of my favorite web sites is Walk Jog Run, a great service that helps you to plan your running routes according to both distance and speed. I use this web site to plot the distance of not only short treks around the block, but also weekend rides which could average as many as 50 miles. Walk Jog Run is particularly friendly because it allows you to interact with the map directly by using the mouse to plot your route by using mouse-clicks to create the course. With each click both the segment and total route distance are calculated, painting a detailed picture of what this particular course will involve.

How might you go about modifying the distance.php script to calculate a route consisting of multiple segments? Other than lifting the restriction regarding the number of allowable markers in the calculateDistance() JavaScript function, the only changes you need to make all reside within the PHP script. Some additional logic will allow the distance of each segment to be calculated, and will tally those segments up to determine the total distance. The modified PHP script follows:

```<?php
// Revert the passed parameter back to an array
\$coordinates = explode("|", \$this->_request->getParam('coords'));
// Initialize the distance
\$distance = 0.0;
// Initialize the coordinate placeholder
\$prevCoordinate = "";
// Cycle through the \$coordinates array, calculating the distance
// between each segment
foreach (\$coordinates AS \$coordinate)
{
// Are we on first array element?
if (\$coordinates[0] == \$coordinate) {
// Convert lat/long into array
\$prevCoordinate = \$coordinate;
} else {
\$coord1 = explode(",", \$coordinate);
\$coord2 = explode(",", \$prevCoordinate);
\$distance += \$this->map->geoGetDistance(\$coord1[0], \$coord1[1], \$coord2[0], \$coord2[1]);
}
}
echo \$distance;
?>
```

Once the new script is in place, you can perform distance calculations such as that shown in Figure 3.

Figure 3. Taking a stroll around Goodale park

Conclusion

Thus concludes the latest installment of this occasional series. If you wind up doing anything interesting with this concept, I'd love to hear about it! Be sure to e-mail me at wjATwjgilmore.com with your comments.

Jason Gilmore is founder of W.J. Gilmore, LLC, a publishing and consulting firm based out of Columbus, Ohio. Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industry’s most respected publishing programs. He’s the author of several books, including the best-selling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional.

Jason is cofounder of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer’s conference.

Page 2 of 2

Comment and Contribute

(Maximum characters: 1200). You have characters left.

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Most Popular Developer Stories

Recommended Partner Resources

Thanks for your registration, follow us on our social networks to keep up-to-date