/** * map at Google * * @link http://www.nabble.com/problem-loading-googlemaps-into-jquery-UI-tabs-td15962881s27240.html * * @param array a list of locations * @param int the scale to use * @param string object width * @param string object height * @return string suitable XHTML to be sent to the browser */ public static function map_on_google($items, $scale = null, $width = null, $height = null) { global $context; // default values if not defined in skin if (!isset($context['skins_gmap_default_width'])) { $context['skins_gmap_default_width'] = '500px'; } if (!isset($context['skins_gmap_default_height'])) { $context['skins_gmap_default_height'] = '300px'; } if (!isset($context['skins_gmap_default_scale'])) { $context['skins_gmap_default_scale'] = '5'; } // default values from the skin if (!$scale) { $scale = $context['skins_gmap_default_scale']; } if (!$width) { $width = $context['skins_gmap_default_width']; } if (!$height) { $height = $context['skins_gmap_default_height']; } // we return some text $text = ''; // we can have several maps on the same page static $map_index; if (!isset($map_index)) { $map_index = 0; $handle = 'map'; // ensure we have a header $text .= Locations::map_on_google_header(); // another name for the next map } else { $map_index++; $handle = 'map_' . $map_index; } // a place holder for the dynamic map $text .= '<div id="' . $handle . '" style="border: 1px solid #979797; background-color: #e5e3df; width: ' . $width . '; height: ' . $height . ';">' . "\n" . ' <div style="padding: 1em; color: gray">' . i18n::s('Loading...') . '</div>' . "\n" . '</div>' . "\n"; // frame the map $latitudes = $longitudes = 0.0; $index = 0; foreach ($items as $id => $attributes) { $latitudes += $attributes['latitude']; $longitudes += $attributes['longitude']; $index++; } // center point $latitude_middle = $latitudes / max(1, $index); $longitude_middle = $longitudes / max(1, $index); // create this map $js_script = 'var mapOptions = {' . "\n" . ' zoom: 13,' . "\n" . ' center: new google.maps.LatLng(parseFloat("' . $latitude_middle . '"), parseFloat("' . $longitude_middle . '")),' . "\n" . ' mapTypeId: google.maps.MapTypeId.ROADMAP' . "\n" . '};' . "\n" . 'var map = new google.maps.Map($("#' . $handle . '")[0], mapOptions);'; // add all markers $index = 1; foreach ($items as $id => $item) { // ensure we have split coordinates if (!$item['latitude'] || !$item['longitude']) { list($item['latitude'], $item['longitude']) = preg_split('/[\\s,;]+/', $item['geo_position']); } // some HTML description for this item $description = ''; if (isset($item['geo_place_name'])) { $description .= $item['geo_place_name'] . BR . "\n"; } if ($item['description']) { $description .= Codes::beautify($item['description']); } // use anchor information if (isset($item['anchor']) && ($anchor = Anchors::get($item['anchor'])) && is_object($anchor)) { // insert thumbnail, if any if ($icon = $anchor->get_thumbnail_url()) { $description = '<a href="' . $context['url_to_root'] . $anchor->get_url() . '"><img src="' . $icon . '" alt="" title="' . encode_field($anchor->get_title()) . '" style="float: left; margin-right: 1em; border: none;" /></a>' . $description; } // a link to the anchor page $description .= BR . "\n" . Skin::build_link($anchor->get_url(), $anchor->get_title()); } // item type if (isset($item['anchor']) && strpos($item['anchor'], 'user:'******'iconRed'; } else { $icon = 'iconBlue'; } // add one marker for this item $js_script .= ' var point = new google.maps.LatLng(parseFloat("' . $item['latitude'] . '"), parseFloat("' . $item['longitude'] . '"));' . "\n" . ' var marker' . $map_index . $index . ' = new google.maps.Marker({ position: point, map: map });' . "\n" . ' var infoWindow = new google.maps.InfoWindow();' . "\n" . 'google.maps.event.addDomListener(marker' . $map_index . $index . ', "click", function() {' . "\n" . ' infoWindow.setContent("' . addcslashes($description, '\'\\"' . "\n\r") . '");' . "\n" . ' infoWindow.open(map, marker' . $map_index . $index . ');' . "\n" . ' });' . "\n" . '$("body").bind("yacs", function(e) {' . "\n" . ' google.maps.event.trigger(map, "resize");' . "\n" . ' map.setZoom( map.getZoom() );' . "\n" . ' map.setCenter(point);' . "\n" . '});' . "\n"; // next index $index++; } // the postamble Page::insert_script($js_script); // job done return $text; }