MAP SCRIPTING 101
Adam DuVander
Copyright 2010
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an "As Is" basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
ACKNOWLEDGMENTS
For a number of years I've kept a personal blog called Simplicity Rules where I've covered ways to raise productivity and decrease stress at the same time. From that frame of reference, I am now able to report that there is no simple way to write a book. However, the following people and organizations made it a whole lot easier for me than it might have been.
My wife, Jenny. You supported me in many ways, including one that I didn't expect: You were always there to encourage me to write the "bockety first draft" of a chapter. Somehow, you knew I'd be able to eventually turn it into something worth reading.
The entire staff of No Starch Press. If there's anybody that makes creating books look easy, it's you guys. Bill Pollock, thanks for getting this thing started; Tyler Ortman, your guidance is immeasurable; Ansel Staton, you made this book look sharp.
LeeAnn Pickerell's copy editing went well beyond finding typos and sentence fragments. Among her many talents, she destroys clichs like they're going out of style. (You can leave this one, LeeAnn.)
Derek Fowler, a major contributor to Mapstraction and the dedicated technical reviewer of this book. Thanks for making me smarter.
Mike Calore, my longtime editor at Wired and Webmonkey. I'm certain that whatever "instincts" I have about my audience are merely lessons you've taught me (perhaps more than once).
Bert Sperling. You got me excited about location in the first place.
The always positive Portland tech community, who never stopped asking about my book, even when I had been writing it for an awkwardly long time. In particular, I'm grateful to my teahouse buddy, Andy Baio. You offered just the balance of encouragement and kick-in-the-pants I needed.
La Bonita, a Mexican restaurant (with delicious cod tacos) where I wrote the bulk of this book. Thanks for making me feel like Coppola.
INTRODUCTION
The Web has changed our lives in many ways. The first online, on-demand driving directions from MapQuest very nearly rendered traditional road atlases obsolete. Today, many websites that provide driving directions also make their maps available to developers. Using these mapping APIs, you can plot your own points or make a mashup with geo-data from other websites.
This book shows you how to take advantage of these services and include their maps on your site. Instead of limiting you to one provider, I'll show you how to use all of them via an open source library called Mapstraction . Write your code once and watch it work in Google Maps, Bing, MapQuest, Yahoo!, OpenStreetMap, and more.
In addition to teaching you how to work with maps from these providers, I'll show you many other common geographic projects. You'll learn how to calculate the distance between locations and embed driving directions on your own site. You'll also learn how to customize the way your map looks by adding your own icons, adding large graphic overlays, or even completely changing the underlying map imagery.
Bringing location to the Web by embedding maps is an important part of most sites now, but there's also an increasing need to bring the Web "on location" to smartphones running mobile browsers. You can add maps to mobile versions of your site using the techniques shown in this book. And I'll show you how to use a convenient geolocation standard to find your user's location, whether he's using a phone, a tablet, or even a regular computer.
You are just pages away from adding some where to your website. This book is designed to help you quickly get to work on an application you already have in mind or inspire your next map. To that end, I've organized the book into projects. And once you become a map scripting wizard, I hope this book will be useful enough as a reference to earn a spot on your bookshelf.
About This Book
The book's project-based approach starts off with basic examples then picks up speed quickly. If you're one to jump ahead, I'd recommend you at least read first. Almost every example in the book builds upon the map you will create in that section.
In you'll learn the basics of constructing online maps. I'll introduce Mapstraction and show how to add controls, such as a zoom interface, to your maps.
In you'll start adding your own points to the map. You'll create custom icons and add message boxes to describe locations.
In you'll learn many ways to convert addresses and city names to coordinates that mapping providers can understand. This process, called geocoding , is a big part of making mapping human-friendly.
In you'll add more complex layers to your map. You'll learn how to draw lines to describe routes and shapes to outline borders. You'll even see how to take large graphics, geo-reference them, and then add them as a map overlay.
In you'll make your maps respond to events, such as drags, clicks, and zooms. These hooks allow you to create an even more interactive experience for your users.
In we'll explore proximity. You'll learn how to create driving directions or search around a point. You'll also dive into some more advanced topics, such as determining whether a location is within a shape (known as a hit test ).
In you'll learn several simple ways to access your user's location with various degrees of accuracy. I'll cover using the geolocation standard, falling back on IP address data, and integrating with location sharing services.
In you'll focus on common location data formats used on the Web. You'll learn to parse GeoRSS, Google Earth's KML, and XML output from most GPS devices.
In it's time to go server side. You'll get a crash course in PHP and MySQL, two technologies provided by many web hosts. We'll then use these languages for common location tasks, such as finding the closest points from your own database.
In you'll put it all together with five fun mashups. You'll create a Twitter tweet finder, an interactive weather map, and a way to find a coffee shop between two locations (so you can meet a friend in the middle). There's also a local concert finder and a way to visualize earthquakes around the world.