Index: trunk/extensions/SemanticGoogleMaps/SGM_Utils.inc |
— | — | @@ -0,0 +1,293 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +if (!defined('MEDIAWIKI')) die(); |
| 5 | + |
| 6 | +/** |
| 7 | + * A class that holds static helper functions for Semantic Google Maps |
| 8 | + * |
| 9 | + * @author Robert Buzink |
| 10 | + * @author Yaron Koren |
| 11 | + */ |
| 12 | + |
| 13 | +class SGMUtils { |
| 14 | + |
| 15 | + /** |
| 16 | + * Render the #semantic_google_map function - the output is HTML |
| 17 | + */ |
| 18 | + function functionRender( &$parser, $coordinates = '1,1', $zoom = '14', $type = 'G_NORMAL_MAP', $control_class = 'GSmallMapControl', $class = 'pmap', $width = '200', $height = '200', $style = '' ) { |
| 19 | + global $wgJsMimeType, $wgGoogleMapsKey, $wgGoogleMapsOnThisPage; |
| 20 | + |
| 21 | + if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
| 22 | + $wgGoogleMapsOnThisPage++; |
| 23 | + |
| 24 | + list($lat, $lon) = SGMUtils::getLatLon($coordinates); |
| 25 | + |
| 26 | + $output =<<<END |
| 27 | +<script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
| 28 | +<script type="text/javascript"> function createMarker(point, label) { var marker = new GMarker(point); GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(label, GInfoWindoOptions.maxWidth=100); }); return marker; } function addLoadEvent(func) { var oldonload = window.onload; if (typeof oldonload == 'function') { window.onload = function() { oldonload(); func(); }; } else { window.onload = func; } } window.unload = GUnload;</script> |
| 29 | +<div id="map$wgGoogleMapsOnThisPage" class="$class" style="$style" ></div> |
| 30 | +<script type="text/javascript"> function makeMap{$wgGoogleMapsOnThisPage}() { if (GBrowserIsCompatible()) {var map = new GMap2(document.getElementById("map{$wgGoogleMapsOnThisPage}"), {size: new GSize('$width', '$height')}); map.addControl(new {$control_class}()); map.addControl(new GMapTypeControl()); map.setCenter(new GLatLng({$lat}, {$lon}), {$zoom}, {$type}); var point = new GLatLng({$lat}, {$lon}); var marker = new GMarker(point); map.addOverlay(marker); } else { document.write('should show map'); } } addLoadEvent(makeMap{$wgGoogleMapsOnThisPage});</script> |
| 31 | + |
| 32 | +END; |
| 33 | + |
| 34 | + return array( $output, 'noparse' => true, 'isHTML' => true ); |
| 35 | + } |
| 36 | + |
| 37 | + function getLatLon($param2) { |
| 38 | + $coordinates = preg_split("/,/", $param2); |
| 39 | + if (count($coordinates) == 2) { |
| 40 | + $lat = SGMUtils::convertCoord($coordinates[0]); |
| 41 | + $lon = SGMUtils::convertCoord($coordinates[1]); |
| 42 | + return array($lat, $lon); |
| 43 | + } |
| 44 | + return array(null, null); |
| 45 | + } |
| 46 | + |
| 47 | + function degree2Decimal($deg_coord="") { |
| 48 | + $dpos=strpos($deg_coord,'°'); |
| 49 | + $mpos=strpos($deg_coord,'.'); |
| 50 | + $spos=strpos($deg_coord,'"'); |
| 51 | + $mlen=(($mpos-$dpos)-1); |
| 52 | + $slen=(($spos-$mpos)-1); |
| 53 | + $direction=substr(strrev($deg_coord),0,1); |
| 54 | + $degrees=substr($deg_coord,0,$dpos); |
| 55 | + $minutes=substr($deg_coord,$dpos+1,$mlen); |
| 56 | + $seconds=substr($deg_coord,$mpos+1,$slen); |
| 57 | + $seconds=($seconds/60); |
| 58 | + $minutes=($minutes+$seconds); |
| 59 | + $minutes=($minutes/60); |
| 60 | + $decimal=($degrees+$minutes); |
| 61 | + //South latitudes and West longitudes need to return a negative result |
| 62 | + if (($direction=="S") or ($direction=="W")) { |
| 63 | + $decimal *= -1; |
| 64 | + } |
| 65 | + return $decimal; |
| 66 | + } |
| 67 | + |
| 68 | + function decDegree2Decimal($deg_coord = "") { |
| 69 | + $direction = substr(strrev($deg_coord), 0, 1); |
| 70 | + $decimal = floatval($deg_coord); |
| 71 | + if (($direction == "S") or ($direction == "W")) { |
| 72 | + $decimal *= -1; |
| 73 | + } |
| 74 | + return $decimal; |
| 75 | + } |
| 76 | + |
| 77 | + function convertCoord($deg_coord = "") { |
| 78 | + if (preg_match('/°/', $deg_coord)) { |
| 79 | + if (preg_match('/"/', $deg_coord)) { |
| 80 | + return SGMUtils::degree2Decimal($deg_coord); |
| 81 | + } else { |
| 82 | + return SGMUtils::decDegree2Decimal($deg_coord); |
| 83 | + } |
| 84 | + } |
| 85 | + return $deg_coord; |
| 86 | + } |
| 87 | + |
| 88 | + function latDecimal2Degree($decimal) { |
| 89 | + if ($decimal < 0) { |
| 90 | + return abs($decimal) . "° S"; |
| 91 | + } else { |
| 92 | + return $decimal . "° N"; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + function lonDecimal2Degree($decimal) { |
| 97 | + if ($decimal < 0) { |
| 98 | + return abs($decimal) . "° W"; |
| 99 | + } else { |
| 100 | + return $decimal . "° E"; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // the function that outputs the custom form html |
| 105 | + function formInputHTML($coordinates, $input_name, $is_mandatory, $is_disabled, $field_args) { |
| 106 | + global $gTabIndex, $gDisabledText, $wgJsMimeType; |
| 107 | + global $wgGoogleMapsKey, $wgGoogleMapsOnThisPage; |
| 108 | + |
| 109 | + // default values |
| 110 | + $flat = 0; |
| 111 | + $flon = 0; |
| 112 | + |
| 113 | + if ($coordinates) { |
| 114 | + // can show up here either as an array or a string, |
| 115 | + // depending on whether it came from user input or a wiki page |
| 116 | + if (is_array($coordinates)) { |
| 117 | + // todo if relevant |
| 118 | + } else { |
| 119 | + list($flat, $flon) = SGMUtils::getLatLon($coordinates); |
| 120 | + } |
| 121 | + $zoom = '14'; |
| 122 | + } else { |
| 123 | + // if there's no starting value, get the value for the |
| 124 | + // map center and zoom from the form input, if they exist |
| 125 | + if (array_key_exists('center', $field_args)) { |
| 126 | + list($flat, $flon) = SGMUtils::getLatLon($field_args['center']); |
| 127 | + $zoom = '14'; |
| 128 | + } else { |
| 129 | + $zoom = '0'; |
| 130 | + } |
| 131 | + if (array_key_exists('zoom', $field_args)) { |
| 132 | + $zoom = $field_args['zoom']; |
| 133 | + } |
| 134 | + } |
| 135 | + if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
| 136 | + $wgGoogleMapsOnThisPage++; |
| 137 | + if (array_key_exists('width', $field_args)) { |
| 138 | + $width = $field_args['width']; |
| 139 | + } else { |
| 140 | + $width = '200'; |
| 141 | + } |
| 142 | + if (array_key_exists('height', $field_args)) { |
| 143 | + $height = $field_args['height']; |
| 144 | + } else { |
| 145 | + $height = '200'; |
| 146 | + } |
| 147 | + $class = 'sm_map'; |
| 148 | + if (array_key_exists('map type', $field_args)) { |
| 149 | + $type = $field_args['map type']; |
| 150 | + } else { |
| 151 | + $type = 'G_NORMAL_MAP'; |
| 152 | + } |
| 153 | + if (array_key_exists('map control', $field_args)) { |
| 154 | + $control_class = $field_args['map control']; |
| 155 | + } else { |
| 156 | + $control_class = 'GSmallMapControl'; |
| 157 | + } |
| 158 | + if ($flat == 0) { $lat = '50';} else {$lat = $flat;} |
| 159 | + if ($flon == 0) { $lon = '5';} else {$lon = $flon;} |
| 160 | + |
| 161 | + // input field |
| 162 | + $starting_coords = ""; |
| 163 | + if ($coordinates != null && $flat != 0 && $flon != 0) { |
| 164 | + $deg_lat = SGMUtils::latDecimal2Degree($flat); |
| 165 | + $deg_lon = SGMUtils::lonDecimal2Degree($flon); |
| 166 | + $starting_coords = "$deg_lat, $deg_lon"; |
| 167 | + } |
| 168 | + $info_id = "info_$gTabIndex"; |
| 169 | + $text =<<<END |
| 170 | + <input tabindex="$gTabIndex" id="input_$gTabIndex" name="$input_name" type="text" value="$starting_coords" size="40" $gDisabledText> |
| 171 | + <span id="$info_id" class="error_message"></span> |
| 172 | + |
| 173 | +END; |
| 174 | + |
| 175 | + // map div |
| 176 | + $text .= '<div id="sm_map'.$wgGoogleMapsOnThisPage.'" class="'.$class.'"></div>'; |
| 177 | + |
| 178 | + //geocoder HTML |
| 179 | + wfLoadExtensionMessages( 'SemanticGoogleMaps' ); |
| 180 | + $lookup_coordinates_text = wfMsg('semanticgooglemaps_lookupcoordinates'); |
| 181 | + $text .= <<<END |
| 182 | + <p> |
| 183 | + <input size="60" id= "geocode" name="geocode" value="" type="text"> |
| 184 | + <a href="#" onClick="showAddress(document.forms['createbox'].geocode.value); return false">$lookup_coordinates_text</a> |
| 185 | + </p> |
| 186 | + <br /> |
| 187 | + |
| 188 | +END; |
| 189 | + |
| 190 | + // map Javascript |
| 191 | + $text .= <<<END |
| 192 | +<script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
| 193 | +<script type="text/javascript"> |
| 194 | +function showAddress(address) { |
| 195 | + makeMap{$wgGoogleMapsOnThisPage}(); |
| 196 | + if (geocoder) { |
| 197 | + geocoder.getLatLng(address, |
| 198 | + function(point) { |
| 199 | + if (!point) { |
| 200 | + alert(address + " not found"); |
| 201 | + } else { |
| 202 | + map.clearOverlays() |
| 203 | + map.setCenter(point, 14); |
| 204 | + var marker = new GMarker(point); |
| 205 | + map.addOverlay(marker); |
| 206 | + document.getElementById("input_$gTabIndex").value = convertLatToDMS(point.y)+', '+convertLngToDMS(point.x); |
| 207 | + } |
| 208 | + } |
| 209 | + ); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +function convertLatToDMS (val) { |
| 214 | + if (val < 0) { |
| 215 | + return Math.abs(val) + "° " + "S"; |
| 216 | + } else { |
| 217 | + return Math.abs(val) + "° " + "N"; |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +function convertLngToDMS (val) { |
| 222 | + if (val < 0) { |
| 223 | + return Math.abs(val) + "° " + "W"; |
| 224 | + } else { |
| 225 | + return Math.abs(val) + "° " + "E"; |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +function addLoadEvent(func) { |
| 230 | + var oldonload = window.onload; |
| 231 | + if (typeof oldonload == 'function') { |
| 232 | + window.onload = function() { |
| 233 | + oldonload(); |
| 234 | + func(); |
| 235 | + }; |
| 236 | + } else { |
| 237 | + window.onload = func; |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +window.unload = GUnload; |
| 242 | +</script> |
| 243 | + |
| 244 | +END; |
| 245 | + $javascript_text = <<<END |
| 246 | +<script type="text/javascript"> |
| 247 | +function makeMap{$wgGoogleMapsOnThisPage}() { |
| 248 | + if (GBrowserIsCompatible()) { |
| 249 | + window.map = new GMap2(document.getElementById("sm_map{$wgGoogleMapsOnThisPage}"), {size: new GSize('$width', '$height')}); |
| 250 | + geocoder = new GClientGeocoder(); |
| 251 | + map.addControl(new {$control_class}()); |
| 252 | + map.addControl(new GMapTypeControl()); |
| 253 | + map.setCenter(new GLatLng({$lat}, {$lon}), {$zoom}, {$type}); |
| 254 | + |
| 255 | +END; |
| 256 | + // show a starting marker only if a value already exists |
| 257 | + if ($coordinates != null) { |
| 258 | + $javascript_text .= <<<END |
| 259 | + var point = new GLatLng({$lat}, {$lon}); |
| 260 | + var marker = new GMarker(point); |
| 261 | + map.addOverlay(marker); |
| 262 | + |
| 263 | +END; |
| 264 | + } |
| 265 | + $javascript_text .= <<<END |
| 266 | + GEvent.addListener(map, "click", |
| 267 | + function(overlay, point) { |
| 268 | + place = null; |
| 269 | + if (overlay) { |
| 270 | + map.removeOverlay (overlay); |
| 271 | + } else { |
| 272 | + var marker = new GMarker (point); |
| 273 | + map.clearOverlays(); |
| 274 | + document.getElementById("input_$gTabIndex").value = convertLatToDMS(point.y)+', '+convertLngToDMS(point.x); |
| 275 | + map.addOverlay(marker); |
| 276 | + map.panTo(point); |
| 277 | + } |
| 278 | + } |
| 279 | + ); |
| 280 | + } |
| 281 | +} |
| 282 | +addLoadEvent(makeMap{$wgGoogleMapsOnThisPage}); |
| 283 | +</script> |
| 284 | + |
| 285 | +END; |
| 286 | + // remove all newlines, to avoid wiki parsing inserting unwanted |
| 287 | + // <p> tags within the Javascript |
| 288 | + $javascript_text = preg_replace('/\s+/m', ' ', $javascript_text); |
| 289 | + $text .= $javascript_text; |
| 290 | + |
| 291 | + $output = array($text, ''); |
| 292 | + return $output; |
| 293 | + } |
| 294 | +} |