Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

MyMap: A Portable API for Maps


MyMap In Action

Listing One is a basic HTML page using MyMap. This example just displays a map at zoom level 6, in "road mode." A marker is placed in the center of the map. To choose the map provider Google Maps, Yahoo Maps, or Virtual Earth, you just need to remove the JavaScript comment around the appropriate include statements. Two include statements are needed—one for the JavaScript of the map provider, and another for the corresponding MyMap API. A better way could be to include the JavaScript for the provider in the MyMap API file, but a simple task such as including a file in another file is not trivial in JavaScript. Furthermore, most of the AJAX frameworks provide one such implementation and a real application could dynamically generate these includes in the web server logic.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>MyMap Sample 1</title>
 <!— Google Maps
 —>
 <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAor5up_1nM9SKM8pR     _DseXBSbhcvIRagSHGG9ZRIE7qvWjC5W0hSk4CSCk-m47NJODtlk_qUex2ZeRw" type="text/javascript"></script>
 <script src="scripts/mymap_google.js" type="text/javascript"></script>

 <!— Yahoo Maps
 <script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=YahooDemo"></script>
 <script src="scripts/mymap_yahoo.js"></script>
 —>

 <!— Virtual Earth
 <script src="http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"></script>
 <script src="scripts/mymap_vearth.js"></script>
 —>

 <script type="text/javascript">
 function load() {
 MyMapInitialize("map", 48.786290, 2.057517, 6, MYMODE_MAP);
 MyMapAddMarker(48.786290, 2.057517, MYMARKER_TYPE1, "<B>I'm here!</B><BR/>");
 }
 function unload() {
 MyMapTerminate();
 }
 </script>
</head>>
<body onload="load()" onunload="unload()">>
 <div id="map" style="position:relative; width: 600px; height: 400px"></div>
</body>>
</html>
Listing One

Google Maps not only references a JavaScript file, but also an encrypted value named "key." This value must be generated by filling in a form on the Google Maps web site. To get this value, you must provide the URL of your web site so that the Google Maps JavaScript checks whether the URL is only used from your web site (or from a local file; for example, "file://..."). Yahoo Maps uses a similar registration process with an "appid" but does not check from where access comes. Registration is not needed for Microsoft Virtual Earth.

Figure 1 is MyMap in a real application. This sample shows JavaScript interaction between the map and the HTML form's radio button and listbox. Moreover, this sample gives a way to dynamically switch between map providers. The application is the official roadmap Santa Claus visits on December 25, 2006. The map shows markers for some famous good and bad boys all around the world. Of course, only good boys were visited by Santa Claus. Listing Two is the JavaScript source code linked to the HTML page. It uses MyMap functions only (no provider-specific function calls).


var guys = [
 { "lat": 48.786290, "lng": 2.057517, "is": "Good", "name":"Lionel Lask\351", "company":"C2S" },
 { "lat": 43.578394, "lng": 1.495859, "is": "Bad", "name":"Sami Jaber", "company":"Valtech" },
 { "lat": 44.261882, "lng": 4.199038, "is": "Bad", "name":"Yacine Chahrour", "company":"Lygue" },
 { "lat": 37.53736, "lng": -122.326571, "is": "Good", "name":"Jon Erickson", "company":"Dr. Dobb's Journal" },
 { "lat": 47.639557, "lng": -122.128336, "is": "Good", "name":"Bill Gates", "company":"Microsoft" },
 { "lat": 37.416987, "lng": -122.025135, "is": "Bad", "name":"Terry Semel", "company":"Yahoo!" },
 { "lat": 37.421690, "lng": -122.084594, "is": "Good", "name":"Larry Page", "company":"Google" }
];
function load() {
 MyMapInitialize("map",42.81152174509788, -49.5703125, 2, MYMODE_MAP);
 for (i=0; i<guys.length; i++) {
 guys[i].marker = null;
 print(i);
 var newopt = document.createElement('option');
 newopt.text = guys[i].name;
 newopt.value = i;
 try {
 document.getElementById("list").add(newopt);
 }
 catch(e) {
 document.getElementById("list").add(newopt, null);
 }
 }
}
function unload() {
 MyMapTerminate();
}
function filter(is) {
 for (i=0; i<guys.length; i++) {
 if (guys[i].is == is)
 hide(i);
 else
 print(i);
 }
}
function print(index) {
 var person = guys[index];
 if (person.marker != null)
 return;
 var markertype = MYMARKER_TYPE1;
 if (person.is == "Bad")
 markertype = MYMARKER_TYPE2;
 guys[index].marker = 
    MyMapAddMarker(person.lat, person.lng, markertype, "<B>"+person.name+"</B><BR/>"+person.company);
}
function hide(index) {
 var person = guys[index];
 if (person.marker != null) {
 MyMapRemoveMarker(person.marker);
 person.marker = null;
 }
}
function gotoselected() {
 goto(document.getElementById("list").value);
}
function goto(index) {
 var person = guys[index];
 MyMapSetZoom(12);
 MyMapGoto(person.lat, person.lng);
}
Listing Two

[Click image to view at full size]

Figure 1: A sample application using MyMap.

As you can see at the beginning of the program, all markers are stored as objects in an array named "guys." The "guy" object is initialized at startup with all existing guys described in JSON (JavaScript Object Notation). The sample has just a few HTML and JavaScript pages so it could work without a web server. But of course, it's possible to replace the JSON initialization directly in the script by something like a web service call (it's precisely what JSON was created for).

The MyMap API is initialized in the load function, then a marker is created with MyMapAddMarker for each person. These are all added to the HTML list for which we handle the selected item event.

MyMapRemoveMarker and MyMapAddMarker are used when filtering the persons to display on the map.

Finally, when the selected item event is raised by the listbox, MyMapSetZoom and MyMapGoto are used to move the map to the chosen boy.

This sample was tested on Google Maps, Yahoo Maps, and Microsoft Virtual Earth both on Internet Explorer and Firefox.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.