Conversion tables A-K
  Conversion tables L-Z
  SPAM database query
  Reverse WHOIS utility
  North America area codes
  PHP Example forums

PHP example repository.

Back to examples
GIS Mapping in PHP: Part 3

This article is a brief note on doing the reverse of the functions described in Article 1. The first article showed how to plot a longitude and latitude coordinate on a map of the world. Part 3 explains how to calculate out what longitude and latitude someone was after when they click on the same map.

Article 1 introduced us to the getlocationcoords function, which was designed to retrieve screen coordinates for a cylindrical projection of the world. The reverse of this function is

function getlocationcoords_inv($x,$y, $width, $height)
   $lat = ((($y / $height) * 180) - 90) * -1;
   $lon =(($x / $width) * 360) - 180;
   return array($lat,$lon);

This function returns longitude and latitude from screen coordinates. Annoyingly as simple as that.

To examine what there is of it a little further - $x and $y are the screen coordinates, $width and $height store the height and width of the source image.

Remember that in this instance we assume the map is a COMPLETE map of the world ( as below ), not a part of it.

So lets put this into a little bit of code.

All we are going to do is create a HTML form and set our world map as an image map. When you click the map, your browser will POST the coordinates clicked back to our script. The coordinates are passed in ${image_name}_x and ${image_name}_y - image_name being defined by the name="image_name" tag within the <input> markup. We then simply parse the 2 coordinates through getlocationcoords_inv, along with the width and height of the image. Along with most of the examples on this site, we assume for the sake of simplicity that register_globals is on.

The code, along with the other HTML needed, in its most basic form is:


   $width = 310;
   $height = 155;
   $values = getlocationcoords_inv($map_x,$map_y, $width,$height);

<form name="form1" method="post" action="">
<input name="map"
Longitude:<?php print($values[1]);?>
Latitude:<?php print($values[0]);?>

And thats it! See the working example at the end of this article.


This piece of code may be small, but it is perfectly formed. The simplicity is such that integrating it with some of the other examples on this site will allow you to create some seemingly complex applications.

You can plot major cities using the code from article 1, calculate objects within a certain distance from that town, display the coordinates in Degree, Minutes and Seconds and so much more, all with 50 lines of code.

The code can also be converted to Java ( I know, I've done it ) .. allowing for dynamic display of coordinates.


Related Links
Microsoft Mappoint