| 1 | = Add a dynamic point to a layer = |
| 2 | |
| 3 | This is a simple example of how to use PHPMapScript. It is intended to be for begginers (as me). We are going to add a point on the position the user clicked on a map. |
| 4 | |
| 5 | On our MapFile, we have blank Point layer called "INLINE", This is the layer where we are going to add our dynamic point. Here you have a sample: |
| 6 | {{{ |
| 7 | LAYER |
| 8 | NAME "INLINE" |
| 9 | TYPE POINT |
| 10 | STATUS ON |
| 11 | CLASS |
| 12 | NAME "0" |
| 13 | STYLE |
| 14 | COLOR 255 0 0 # red |
| 15 | MAXSIZE 300 # default 50 |
| 16 | SYMBOL 'phone' #In my case was a nice phone image symbol |
| 17 | SIZE 16 |
| 18 | END #style |
| 19 | END #class |
| 20 | END #layer |
| 21 | }}} |
| 22 | = PHP code = |
| 23 | |
| 24 | The source filename is query2.php |
| 25 | {{{ |
| 26 | <?php |
| 27 | |
| 28 | //Load the dynamic library. |
| 29 | dl('php_mapscript.so'); |
| 30 | //dl('php_mapscript_42.dll'); //For WinDoS Users |
| 31 | |
| 32 | //Convert pixels to map units (got from PHPMapscriptSnippet1) |
| 33 | function click2map ($click_x, $click_y) { |
| 34 | global $map; |
| 35 | $e= &$map->extent; //for saving writing |
| 36 | $x_pct = ($click_x / $map->width); |
| 37 | $y_pct = 1 - ($click_y / $map->height); |
| 38 | $x_map = $e->minx + ( ($e->maxx - $e->minx) * $x_pct); |
| 39 | $y_map = $e->miny + ( ($e->maxy - $e->miny) * $y_pct); |
| 40 | |
| 41 | return array($x_map, $y_map); |
| 42 | } |
| 43 | |
| 44 | $map= ms_newMapObj('data/test2.map'); |
| 45 | //Convert pixels to map units |
| 46 | $map_pt = click2map($_GET['image_x'],$_GET['image_y'],$map->extent); |
| 47 | //Create the point |
| 48 | $pt = ms_newPointObj(); |
| 49 | $pt-> setXY($map_pt[0],$map_pt[1]); |
| 50 | |
| 51 | //Draw the map and add the point |
| 52 | $img = $map->draw(); |
| 53 | $layer = $map->getLayerByName('INLINE'); |
| 54 | $pt->draw($map, $layer, $img, 0 ,'yuhuu'); |
| 55 | $url = $img->saveWebImage(); |
| 56 | ?> |
| 57 | |
| 58 | < html>< head></ head>< body> |
| 59 | |
| 60 | <form method="get" action="query2.php" > |
| 61 | <input style="border:thin solid black;" type="image" |
| 62 | name="image" src="<?php echo $url?>" /> |
| 63 | </form> |
| 64 | |
| 65 | < pre> |
| 66 | <!-- Debug Lines --> |
| 67 | <?php |
| 68 | echo "GET\n"; |
| 69 | print_r($_GET); |
| 70 | echo "Point:\n"; |
| 71 | print_r($pt); ?> |
| 72 | ?> |
| 73 | < /pre> |
| 74 | < /body> |
| 75 | < /html> |
| 76 | }}} |
| 77 | -- Merlos |
| 78 | |
| 79 | Go back to: [wiki:PHPMapScript] |