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


Callbacks

Because interactive maps involve user events and asynchronous processing, all map providers make extensive use of callbacks. MyMapGeocode uses a callback to get the geocoding result.

Google and Yahoo let you handle callbacks using a dedicated class (GEvent or YEvent). This class is responsible for registering all callbacks for an object like a map or a marker.

Example 1(a) shows how to create a callback with Google Maps. The method GEvent.addListener is used to associate an object, an event name, and a function to call for that event. Google Maps calls at runtime the registered function of the appropriate object when this event is raised.

(a)
GEvent.addListener(object, eventname, callback);

(b)
YEvent.Capture(object, EventsLists.value, callback);

Example 1: (a) Callback registering in Google Maps; (b) Callback registering in Yahoo Maps.

Example 1(b) shows callback creation in Yahoo Maps. The process is similar to the Google one except that we use an event enumeration instead of an event name.

There is no concept of callback registration in Virtual Earth. Each function directly takes a callback as a parameter when needed. The drawback is that you can't choose which event to use—you are limited to the event the designer of the method chose.

Markers

Again, Google Maps and Yahoo Maps are similar for markers creation. You create an icon object (GIcon/YImage), and then create a marker object (GMarker/YMarker) to hold the marker data. If you want a tooltip to be displayed when the marker is clicked, you simply register a callback on the click event as previously explained.

Virtual Earth uses a single object for the whole process. You need to build a VEPushPin object with an image and some text for the tooltip. It's not possible to choose the triggering event. You can also call custom code neither before nor after the tooltip is displayed.

To avoid icon file handling, MyMap uses the same image for all map providers. You just need to specify a "marker type" constant, which is mapped to a specific icon.

Because all providers support HTML tags in the tooltip text, you can customize your description using formatting, images, or links.

Zoom

Hopefully, zoom levels for all map providers have nearly the same range:

  • From 0 to 19 for Google Maps.
  • From 1 to 16 for Yahoo Maps.
  • From 1 to 19 for Virtual Earth.

Furthermore, the same zoom level gives the same map scale for all providers. It can't be luck!

A slight difference for Yahoo Maps is that zoom levels are inverted: 1 is the higher zoom, 16 is the lower. The MyMapSetZoom function in mymap_yahoo.js does a difference to 18 on its parameter, so you can keep the same function call regardless of which provider you use.


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.