Blog - Latest News

2012/10/20
20 Oct 2012

How to interpolate GPS coordinates on a map

You have GPS coordinates of a city you want to display on a JPG map. It is easy. You just have to measures these 4 key values :
20121020-135642.jpg

Then replace these values in the PHP function below :

<?php

function get_pixel_coordinates($my_city_lat,$my_city_lng) {
	// Map size in pixels
	$map_width  = 1050;
	$map_height = 600;

	// The left-most city on the map
	$bre_x      = 10; 		// pixels from the left of the image
	$bre_lng    = -4.500;	// Longitude

	// The right-most city on the map
	$str_x      = 1000;		// pixels from the left of the image
	$str_lng    = 7.750;	// Longitude

	// The top-most city on the map
	$lil_y      = 10; 		// pixels from the top of the image
	$lil_lat    = 50.650;	// Latitude

	// The bottom-most city on the map
	$mar_y      = 1000; 	// pixels from the top of the image
	$mar_lat    = 43.300;	// Latitude

	$my_city_x = round(($my_city_lng-$bre_lng) * ($str_x-$bre_x) / ($str_lng-$bre_lng));
	$my_city_y = round(($my_city_lat-$mar_lat) * ($mar_y-$lil_y) / ($lil_lat-$mar_lat));

	return array($my_city_x,$my_city_y);
}

/**
 * I know GPS coordinates of a city
 * I want to get its pixel coordinates on my map image
 **/
$my_city_lat = 45.783;
$my_city_lng = 3.083;

echo implode('x',get_pixel_coordinates($my_city_lat,$my_city_lng));

echo "\n";

Take cities near borders of your map to increase precision.

Tags: , ,
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

*