I have Googled myself to death.. I am attempting to write 2 php functions that will return X and Y from Lat and Long, in both Mercator and flat non-projected (grid) maps. Pr
The proportional approach in the previous answer won't work. Mercator projections are quite non-linear.
Here's how I overlay generated images onto a Google or Bing map. In my case, I'm creating a GD image of polygons that will be the overlay. It's much faster to do the polygons in the GD library than the map providers APIs.
First, set up scaling from a standard latitude longitude to a WGS84 projection. Degrees to mercator x-y coordinates in meters.
http://gisgeography.com/wgs84-world-geodetic-system/
// $minlat = minimum image latitude
// $minlon = minimum image longitude
// $maxlat = maximum image latitude
// $maxlon = maximum image longitude
// $latbounds = Image height (in pixels)
// $lonbounds = Image width (in pixels)
$lonrange = abs($maxlon - $minlon);
$WGS84min = log(tan((90.+$minlat)*M_PI/360.))/(M_PI/180.);
$WGS84min = (int) ($WGS84min * 2037598.34/180);
$WGS84max = log(tan((90.+$maxlat)*M_PI/360.))/(M_PI/180.);
$WGS84max = (int) ($WGS84max * 2037598.34/180);
$WGS84diff = $WGS84max - $WGS84min;
$WGS84factor = $latbounds/$WGS84diff;
Then for each latitude/longitude I want to calculate the actual X-Y coordinates on the image.
// $lon1 = the longitude of the point to be converted into image coordinates
// $lat1 = the latitude of the point to be converted into image coordates
X is easy
$x = (int) ((abs($lon1-$minlon)/$lonrange)*$lonbounds);
Y is a bit harder, first calculating to WGS84, and then mapping to the image. Last step, inverting the Y coordinates since the display order is upside down.
$y1 = log(tan((90.+$lat1)*M_PI/360.))/(M_PI/180.);
$y1 = $y1 * 2037598.34/180;
$y1 = (int) (($y1- $WGS84min)*$WGS84factor);
$y = $latbounds - $y1;
when the image file is complete, use GD to save the image and then use the example in the API library to display your overlay.
https://developers.google.com/maps/documentation/javascript/examples/overlay-simple