Changes between Initial Version and Version 1 of PHPMapScriptDynamicLine


Ignore:
Timestamp:
Jan 27, 2009 9:48:34 AM (11 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PHPMapScriptDynamicLine

    v1 v1  
     1= Adding Dynamic Lines with PHPMapScript =
     2
     3Here we have another sample code to illustrate the usage of PHPMapScript. It allows an user to draw a line on the fly. Each time the user clicks on the image it is added that point to a line, then the line is drawn on the map.
     4
     5Basically, the tasks this script does are:
     6
     7   1. Create the map
     8   2. Create a line
     9   3. Add last point to the line
     10   4. Create the shape
     11   5. Add the line to the shape
     12   6. Add the shape to the layer
     13   7. Draw the map
     14
     15This is the layer "lines" used in the code:
     16{{{
     17  LAYER
     18    GROUP "user"
     19    NAME "lines"
     20    TYPE line
     21    STATUS on
     22   CLASS
     23       NAME "0"
     24       TEMPLATE "ttt_query.html"
     25       STYLE
     26         COLOR 255 0 0 # red
     27       END #style
     28   END #class
     29  END #layer
     30}}}
     31== PHP code ==
     32
     33The file is called query3.php
     34{{{
     35<?php
     36
     37dl('php_mapscript.so');
     38//dl('php_mapscript_42.dll'); //For WinDoS Users
     39
     40//Convert pixels to map units got from PHPMapscriptSnippet1
     41function click2map ($click_x, $click_y) {
     42    global $map;
     43    $e= &$map->extent; //Tip for saving type time
     44    $x_pct = ($click_x / $map->width);
     45    $y_pct = 1 - ($click_y / $map->height);
     46    $x_map = $e->minx + ( ($e->maxx - $e->minx) * $x_pct);
     47    $y_map = $e->miny + ( ($e->maxy - $e->miny) * $y_pct);
     48
     49    return array($x_map, $y_map);
     50}
     51
     52
     53// --------------- MAIN -----------------------
     54
     55$map= ms_newMapObj('data/test2.map');
     56
     57//Avoid register global=Off (that way it is not neccesary use $_POST[])
     58import_request_variables("gP", "");
     59
     60if (isset($image_x) && isset($image_y)){
     61    //Convert pixels to map units
     62    $map_pt = click2map($image_x,$image_y);
     63
     64    //Construct the line
     65    $line = ms_newLineObj();
     66    $line->addXY($map_pt[0],$map_pt[1]);
     67    if (is_array($prev_x)) { //Add points of the hidden input fields
     68        foreach ($prev_x as $i => $x){
     69            $line->addXY($x, $prev_y[$i]);
     70        }
     71    }
     72    //Create the shape object that will be added to the layer
     73    $shp = ms_newShapeObj(MS_SHAPE_LINE);
     74    $shp-> add($line);
     75
     76    $layer = $map->getLayerByName('lines');
     77    $layer->addFeature($shp);
     78}
     79//Draw the map
     80$img = $map->draw();
     81$url = $img->saveWebImage();
     82
     83//Now add hidden fields to the form
     84for($i=0;$i<$line->numpoints;$i++) {
     85    $pt = $line->point($i);
     86    $x = $pt->x;
     87    $y = $pt->y;
     88    $HTML_HIDDEN.="<input type=\"hidden\" name=\"prev_x[$i]\" value=\"$x\"/>";
     89    $HTML_HIDDEN.="<input type=\"hidden\" name=\"prev_y[$i]\" value=\"$y\"/>";
     90}
     91?>
     92
     93< html>< head>< title>Yapig</ title></ head>
     94<!--remove white spaces of the HTML tags -->
     95< body>
     96<form method="post" action="query3.php" >
     97   <input style="border:thin solid black;"type="image" name="image" src="<?php echo $url ?>" />
     98     <?php echo $HTML_HIDDEN ?>
     99</form>
     100<a href="query3.php">Clear</a>
     101 </ body>
     102</ html>
     103}}}
     104-- Merlos
     105
     106Go to [wiki:PHPMapScript]