0
<?php
header("Content-type:image/png");
$image1 = imagecreatefrompng('cylindrical_map.png'); 
$image2 = imagecreatefrompng('pin.png'); 

$long = 18.96;
$lat = 72.82;
$scale_x = imagesx($image1);
$scale_y = imagesy($image1);
$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);

imagecopymerge($image1, $image2, $pt["x"],$pt["y"], 0, 0, 80, 80, 98); //// place a second image on top of image 1


imagepng($image1);

function getlocationcoords($lat, $lon, $width, $height)
{  
    $x = (($lon + 180) * ($width / 360));
    $y = ((-1 * $lat)+90) * ($height / 180);
    return array("x"=>round($x),"y"=>round($y));
}

?>

I have this code that convert the lat and long to coordinates but for example i put and lat and long of New york city it wont point on the right location, having trouble with the converting the lat and long to coordinates.

please help me out with this.

Any help is appreciated.

webdev002
  • 13
  • 6
  • How accurate do you need it to be? There are different datums for the size and shape of the planet. – spraff Dec 31 '13 at 02:12
  • Have a look at this SO question: http://stackoverflow.com/questions/8175265/converting-latitude-longitude-coordinates-to-image-map-coordinates it's C# but the link in the accepted answer shows how to convert the coordinates. – Pixelmonster Dec 31 '13 at 02:15
  • accurate as much as possible.. the size of the map is 700 x 500.. – webdev002 Dec 31 '13 at 02:25
  • It's really important to know which shape your map has, because the calculations are different. If it's a equirectangular projection it's quite simple. http://bit.ly/19DJATK Other projections: http://en.wikipedia.org/wiki/List_of_map_projections – Pixelmonster Dec 31 '13 at 02:43
  • the projection of the map is Equirectangular cylindrical_map .. – webdev002 Dec 31 '13 at 02:50

1 Answers1

0

You can use a PEAR package or this SO answer to calculate your coordinates.

The SO thread includes the code for your needs.


Finally, I got a working example for you. (Took a few minutes). I suppose you took this article and changed it a bit.

At first, you need to calculate the offset of the pin. I took this one from a jQuery plugin (I shrinked it down to 50px widthness). This means that you take the x and y coordinate from the tip. Please note that you have to take the upper left point as origin!

$pinOffsetX = 19;
$pinOffsetY = 42;

Now you write down your coordinates. I took the coords for NYC.

$long = -74.0059700;
$lat = 40.7142700;

Load your images. I took this map to have a large map and check the accuracy.

$pin = imagecreatefrompng("pin.png");
$im = imagecreatefromjpeg("map.jpg");

Get the sizes of the images and calculate the coords

$scale_x = imagesx($im);
$scale_y = imagesy($im);
$pinWidth = imagesx($pin);
$pinHeight = imagesy($pin);
$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);

The function was not changed. Now copy the pin onto the map. Notice: I use imagecopy instead of imagecopymerge.

imagecopy($im, $pin, $pt["x"] - $pinOffsetX, $pt["y"] - $pinOffsetY, 0, 0, $pinWidth, $pinHeight);

The pin Offset has to be subtracted to move the tip onto the location. Output the image (and destroy it!)

header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);
Community
  • 1
  • 1
Pixelmonster
  • 396
  • 7
  • 15
  • i already look at that SO the problem is where does the--> `$long_at_left=//Longitude of left-hand coordinates $long_at_right=//Longitude of right-hand coordinates $lat_at_left=//Latitude of left-hand coordinates $lat_at_right=//Latitude of right-hand coordinates` – webdev002 Dec 31 '13 at 04:12
  • Hope my example helps. – Pixelmonster Dec 31 '13 at 06:04
  • Thank you so much. I really appreciated your help.... can i copy the whole CODE?.. if you dont mind :P.. thanks again.. – webdev002 Dec 31 '13 at 06:41
  • can i see the output of the images of yours with the pin and map? – webdev002 Dec 31 '13 at 07:06
  • Here's the complete package with a screenshot: https://www.dropbox.com/s/oc2r4ki5c588kds/map_projection.zip – Pixelmonster Dec 31 '13 at 20:18
  • Yes, It works Thank you very much, you really make my new year happy :P.. sorry i can't vote up coz my reputation right row is low... – webdev002 Jan 03 '14 at 03:12
  • Hi i have a other post can you help me out?.. http://stackoverflow.com/questions/20941929/convert-long-and-lat-to-coordinates-with-japan-map-only?noredirect=1#comment31460144_20941929 – webdev002 Jan 07 '14 at 02:22