To convert the long/lat to screen coordinate we have a created a function called
getlocationcoords. This takes the longitude and latitude coordinates
plus the size of the base map and return the screen coordinates in an associative
$height are calculated from the
size of the background image. In future projects these variables are used scale
the map and set the zoom level.
function getlocationcoords($lat, $lon, $width, $height)
$x = (($lon + 180) * ($width / 360));
$y = ((($lat * -1) + 90) * ($height / 180));
Once the coordinates have been converted it's as simple as drawing a rectangle
on the base map using the returned array to mark our location.
So, the code to create the map:
// These are the coordinates the location we wish to plot.<br>
// These are being passed in the URL, but we will set them to a
// default if nothing is passed.
if(empty($long))$long = -63.10774861954596;
if(empty($lat)) $lat = 46.2899306519141;
// First we load the background/base map. We assume it's located in same dir
// as the script.
// This can be any format but we are using JPG in this example
// We will also allocate the color for the marker
$im = imagecreatefromjpeg("earth_310.jpg");
$red = imagecolorallocate ($im, 255,0,0);
// Next need to find the base image size.
// We need these variables to be able scale the long/lat coordinates.
$scale_x = imagesx($im);
$scale_y = imagesy($im);
// Now we convert the long/lat coordinates into screen coordinates
$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);
// Now mark the point on the map using a red 4 pixel rectangle
// Return the map image. We are using a PNG format as it gives better final
image quality than a JPG
header ("Content-type: image/png");
To load the map call the PHP code from an IMG tag, passing the longitude and
latitude in the long and lat variables:
You could easily modify this code to read long/lat coordinates from a database
or a delimited text file and plot multiple points.
This is a very basic script. However the basic principle will allow you to draw
very complicated maps. The important part is the
function - once you have geocoded your data and have the routines to plot the
points on the screen, the sky's the limit!