I'm working on plotting a set of points on a map dynamically with PHP. I'm using this article for reference and as base code: http://www.phpbuilder.com/columns/moss20031023.php3?page=4

My problem is that I want to use my own custom map of the US, which is located here: http://www.raptranslations.com/misc/usmap.png

Does anyone know exactly how I'd modify the code to work with my map? I've tried several different ways but nothing seems to work quite well. Thanks!

    Hi,

    how does your code look like ?

    What type of locations/points do you want to mark ?

    Thomas

      My code is pretty much the exact same as the one in the link I provided... I tried modifying it to use it with my custom map, but it didn't work and I reverted it back to the original script from this article: http://www.phpbuilder.com/columns/moss20031023.php3?page=1

      I will basically already have longitude & latitude points which should make plotting them relatively easy... I just don't know how to modify that script to work with my custom map of just the USA.

        Ok,

        try something like this:

        <?php
        function getPictureCoordinates($value,$lanlonstart,$lanlonend,$scale) {
        /*
          yt-value = a;
          value-yb = b;
          div = a/b
        
          pic:
        
          -value = a
          value-yb = b;
           -value/(value-yb) = (a/b)
           -value = (a/b)*(value-yb);
           -value = (a/b)*value-((a/b)*yb);
           (a/b)*yb = ((a/b)+1)*value;
           value = (a/b)*yb
                   --------
                   (a/b)+1
        */  
        
        
          $a = $lanlonstart-$value;
          $b = $value-$lanlonend;
          $div = $a/$b;
        
          $platlon = ($div*$scale)/($div+1);
        
          return $platlon;
        }
        
        function getlocationcoords($lat, $lon, $width, $height,$lattop,$latbottom,$lonleft,$lonright)
        {
            $x = getPictureCoordinates($lon,$lonleft,$lonright,$width);
            //$x = (($lon + 180) * ($width / 360));
            $y = getPictureCoordinates($lat,$lattop,$latbottom,$height);
            return array("x"=>round($x),"y"=>round($y));
        }
        // 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.
        $lattop = 30.543;
        $latbottom = -2.54343243;
        $lonleft = -95.32332;
        $lonright= +120.4322;
        
        if(empty($long))$long = -80;
        if(empty($lat)) $lat = 10;
        
        // 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 = imagecreatefrompng("usmap.png");
        $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,$lattop,$latbottom,$lonleft,$lonright);
        
        // Now mark the point on the map using a red 4 pixel rectangle
        imagefilledrectangle($im,$pt["x"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$red);
        
        // 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");
        imagepng($im);
        imagedestroy($im);
        ?>
        

        You need to get the left most and right most longitude and the top most and bottom most latidude of your map. Set them in the PHP script:

        $lattop = 30.543;
        $latbottom = -2.54343243;
        $lonleft = -95.32332;
        $lonright= +120.4322;

        These are no real world values, just a test.

        It seemed to work on my server. I hope I didn't mix up longitude and latidude 😉

        Thomas

          Forgot one point (just to make sure): You'll see nothing if the longitude/latitude values of the mark are not within $lattop and $latbottom ($lonleft and $lonright).

          Thomas

            Write a Reply...