Index: trunk/extensions/SemanticGoogleMaps/SGM_QueryPrinter.php |
— | — | @@ -4,6 +4,8 @@ |
5 | 5 | * Print query results in a Google Map. Based on Google Maps API code |
6 | 6 | * written by Robert Buzink and query results printing code written by |
7 | 7 | * Markus Kr�tzsch. |
| 8 | + * |
| 9 | + * @author Yaron Koren |
8 | 10 | */ |
9 | 11 | |
10 | 12 | class SGMResultPrinter extends SMWResultPrinter { |
— | — | @@ -20,18 +22,18 @@ |
21 | 23 | $result = ""; |
22 | 24 | |
23 | 25 | $locations = array(); |
| 26 | + $legend_labels = array(); |
24 | 27 | // print all result rows |
25 | 28 | while ( $row = $res->getNext() ) { |
26 | | - $lat = $lon = $title = $text = ""; |
27 | | - foreach ($row as $field) { |
| 29 | + $lat = $lon = $title = $text = $icon = ""; |
| 30 | + foreach ($row as $i => $field) { |
28 | 31 | $pr = $field->getPrintRequest(); |
29 | | - $first = true; |
30 | 32 | while ( ($object = $field->getNextObject()) !== false ) { |
31 | 33 | if ($object->getTypeID() == '_geo') { // use shorter "LongText" for wikipage |
32 | 34 | // don't add geographical coordinates to the display |
33 | 35 | } elseif ($object->getTypeID() == '_wpg') { // use shorter "LongText" for wikipage |
34 | 36 | $text .= $pr->getHTMLText($skin) . " " . $object->getLongText($outputmode, $skin) . "<br />"; |
35 | | - if ($first) { |
| 37 | + if ($i == 0) { |
36 | 38 | $title = $object->getShortWikiText(false); |
37 | 39 | } |
38 | 40 | } else { |
— | — | @@ -41,15 +43,38 @@ |
42 | 44 | list($lat,$lon) = explode(',', $object->getXSDValue()); |
43 | 45 | |
44 | 46 | } |
45 | | - $first = false; |
46 | 47 | } |
47 | | - if ($lat != '' && $lon != '') { |
48 | | - $locations[] = array($lat, $lon, $title, $text); |
| 48 | + } |
| 49 | + if ($lat != '' && $lon != '') { |
| 50 | + // look for display_options field, which can |
| 51 | + // be set by Semantic Compound Queries |
| 52 | + if (is_array($row[0]->display_options)) { |
| 53 | + if (array_key_exists('icon', $row[0]->display_options)) { |
| 54 | + $icon = $row[0]->display_options['icon']; |
| 55 | + // this is somewhat of a hack - if a |
| 56 | + // legend label has been set, we're |
| 57 | + // getting it for every point, |
| 58 | + // instead of just once per icon |
| 59 | + if (array_key_exists('legend label', $row[0]->display_options)) { |
| 60 | + $legend_label = $row[0]->display_options['legend label']; |
| 61 | + if (! array_key_exists($icon, $legend_labels)) { |
| 62 | + $legend_labels[$icon] = $legend_label; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + // icon can be set even for regular, non-compound |
| 67 | + // queries -if it is, though, we have to translate |
| 68 | + // the name into a URL here |
| 69 | + } elseif (array_key_exists('icon', $this->m_params)) { |
| 70 | + $icon_title = Title::newFromText($this->m_params['icon']); |
| 71 | + $icon_image_page = new ImagePage($icon_title); |
| 72 | + $icon = $icon_image_page->getDisplayedFile()->getURL(); |
49 | 73 | } |
| 74 | + |
| 75 | + $locations[] = array($lat, $lon, $title, $text, $icon); |
50 | 76 | } |
51 | 77 | } |
52 | 78 | |
53 | | - |
54 | 79 | $coordinates = '1,1'; |
55 | 80 | $class = 'pmap'; |
56 | 81 | if (array_key_exists('width', $this->m_params)) { |
— | — | @@ -90,8 +115,14 @@ |
91 | 116 | $map_text = <<<END |
92 | 117 | <script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
93 | 118 | <script type="text/javascript"> |
94 | | -function createMarker(point, title, label) { |
95 | | - var marker = new GMarker(point, {title:title}); |
| 119 | +function createMarker(point, title, label, icon) { |
| 120 | + if (icon!='') { |
| 121 | + var iconObj = new GIcon(G_DEFAULT_ICON); |
| 122 | + iconObj.image = icon; |
| 123 | + var marker = new GMarker(point, {title:title, icon:iconObj}); |
| 124 | + } else { |
| 125 | + var marker = new GMarker(point, {title:title}); |
| 126 | + } |
96 | 127 | GEvent.addListener(marker, 'click', |
97 | 128 | function() { |
98 | 129 | marker.openInfoWindowHtml(label, {maxWidth:350}); |
— | — | @@ -168,11 +199,11 @@ |
169 | 200 | } |
170 | 201 | // add a marker to the map for each location |
171 | 202 | foreach ($locations as $i => $location) { |
172 | | - list($lat, $lon, $title, $label) = $location; |
| 203 | + list($lat, $lon, $title, $label, $icon) = $location; |
173 | 204 | $title = str_replace("'", "\'", $title); |
174 | 205 | $label = str_replace("'", "\'", $label); |
175 | 206 | $map_text .=<<<END |
176 | | - map.addOverlay(createMarker(new GLatLng($lat, $lon), '$title', '$label')); |
| 207 | + map.addOverlay(createMarker(new GLatLng($lat, $lon), '$title', '$label', '$icon')); |
177 | 208 | END; |
178 | 209 | } |
179 | 210 | |
— | — | @@ -185,6 +216,20 @@ |
186 | 217 | // to avoid wiki parsing adding random '<p>' tags, we have |
187 | 218 | // to replace all newlines with spaces |
188 | 219 | $map_text = preg_replace('/\s+/m', ' ', $map_text); |
| 220 | + |
| 221 | + // add the legend, if any labels have been defined |
| 222 | + if (count($legend_labels) > 0) { |
| 223 | + $map_text .= "\n<div style=\"margin: 10px; padding: 5px;\">\n"; |
| 224 | + foreach ($legend_labels as $icon => $legend_label) { |
| 225 | +/* |
| 226 | + $icon_title = Title::newFromText($icon); |
| 227 | + $icon_image = new ImagePage($icon_title); |
| 228 | + $icon_url = $icon_image->getDisplayedFile()->getURL(); |
| 229 | +*/ |
| 230 | + $map_text .= "<img src=\"$icon\" /> $legend_label "; |
| 231 | + } |
| 232 | + $map_text .= "</div>\n"; |
| 233 | + } |
189 | 234 | $result .= $map_text; |
190 | 235 | |
191 | 236 | // print further results footer |