Home arrow HTML arrow Page 2 - Clickable Geographical Map Regions
HTML

Clickable Geographical Map Regions


When your site has web pages with geographical maps, sometimes it is nice to make the regions of the map clickable. Imagine that you have a political map of Western Europe. It would be nice to make the countries clickable, so that when a user clicks a country, he would go to a page that has political information (e.g. history) about the country. We can use HTML image-maps to achieve this, but the shapes of countries are irregular. How can we turn an irregular shape into a clickable region? Keep reading.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
June 03, 2008
TABLE OF CONTENTS:
  1. · Clickable Geographical Map Regions
  2. · Relationship between Geographical Regions and Image-Maps
  3. · Code Explained
  4. · Caution, Animation, Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Clickable Geographical Map Regions - Relationship between Geographical Regions and Image-Maps
(Page 2 of 4 )

Realize that the outline of a geographical map looks like an irregular polygon. So to make a region of a geographical map clickable, think of its outline as an irregular polygon, then look for the coordinates of the vertices of this outline. In the HTML document, these coordinates will form the coordinates of an AREA element with a polygon shape. The AREA element is, of course, part of the image-map. To make all the geographical regions of the map clickable, the MAP element will consist of AREA elements with polygon shapes and each AREA element will have the coordinates of the region.

Creating coordinates

If you look at the map of any country, you will notice that the number of vertices and, hence, the number of coordinate pairs are many. In this section, I show you how you can get the coordinates using JavaScript. We shall use the map of Western Europe below to illustrate this.




We shall make the regions of Portugal, Spain and France clickable.

In terms of tools for this project, you need a text editor (notepad) and a browser.

Using your text editor, copy and paste the following and save the file with the name "coords.htm" in a directory. In this same directory, save the image of the map of Western Europe with the name europeMap.jpg (beware of the image type).


<html>


<head>

<style type="text/css">

body {margin:0px; padding:0px}

</style>

<script type="text/javascript">

var coordStr = ""

function createCoords(event)

{

x=event.clientX;

y=event.clientY;


if (coordStr == "")

{//the first time we start without a comma

coordStr = coordStr + x + "," +y ;

}

else

{

coordStr = coordStr + "," + x + "," +y ;

}

}

 

function showCoords(event)

{

if (event.button==2)

{

myWindow=window.open();

myWindow.document.write(coordStr);

 

//reset the string

coordStr = "";

myWindow.focus();

}

}

</script>

</head>


<body onclick="createCoords(event)" onmousedown="showCoords(event)">

<img src="europeMap.jpg">

</body>


</html>


blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials