1 : <?php
2 : /**
3 : * This file is part of NoiseLabs-PHP-ToolKit
4 : *
5 : * NoiseLabs-PHP-ToolKit is free software; you can redistribute it
6 : * and/or modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 3 of the License, or (at your option) any later version.
9 : *
10 : * NoiseLabs-PHP-ToolKit is distributed in the hope that it will be
11 : * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 : * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with NoiseLabs-PHP-ToolKit; if not, see
17 : * <http://www.gnu.org/licenses/>.
18 : *
19 : * Copyright (C) 2011 Vítor Brandão
20 : *
21 : * @category NoiseLabs
22 : * @package GoogleAPI
23 : * @author Vítor Brandão <noisebleed@noiselabs.org>
24 : * @copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
25 : * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL-3
26 : * @link http://www.noiselabs.org
27 : * @since 0.1.0
28 : */
29 :
30 : namespace NoiseLabs\ToolKit\GoogleAPI\Maps;
31 :
32 : use NoiseLabs\ToolKit\GoogleAPI\Maps\BaseMap;
33 :
34 : /**
35 : * A class that makes use of the Google Maps API to create customizable maps
36 : * that can be embedded on your website.
37 : *
38 : * The API version in use is Google Maps Javascript API Version 3.
39 : * @link http://code.google.com/intl/pt-PT/apis/maps/documentation/javascript/basics.html
40 : */
41 : class Map extends BaseMap
42 0 : {
43 : /**
44 : * For backwards compatibility.
45 : */
46 : public function printGoogleJS()
47 : {
48 0 : return $this->loadJavascriptApi();
49 : }
50 :
51 : /**
52 : * Include the Maps API JavaScript using a script tag.
53 : *
54 : * This function should be called in between the html <head></head> tags.
55 : */
56 : public function loadJavascriptApi()
57 : {
58 0 : $parameters = $this->parameters->all();
59 0 : $html = '<script type="text/javascript" src="';
60 :
61 : /**
62 : * Load via HTTPS?
63 : *
64 : * Loading the Google Maps Javascript API V3 over HTTPS allows your
65 : * application to use the Maps API within pages that are secured using
66 : * HTTPS: the HTTP over the Secure Socket Layer (SSL) protocol.
67 : *
68 : * Loading the Maps API over HTTPS is necessary in SSL applications to
69 : * avoid security warnings in most browsers, and is recommended for
70 : * applications that include sensitive user data, such as a user's
71 : * location, in requests.
72 : */
73 0 : if (true === $this->options->get('https', false)) {
74 0 : $html .= 'https://maps-api-ssl.google.com/maps/api/js?v=3';
75 0 : }
76 : else {
77 0 : $html .= 'http://maps.google.com/maps/api/js';
78 0 : if (!empty($parameters)) { $html .= '?'; }
79 : }
80 :
81 : // include defined parameters like 'sensor' or 'language' (if available)
82 0 : foreach ($parameters as $key => $value) {
83 0 : $html .= $key.'='.$value.'&';
84 0 : }
85 0 : $html = rtrim($html, '&'); // remove the last ampersand ('orphan')
86 0 : $html .= '"></script>';
87 :
88 0 : echo $html;
89 0 : }
90 :
91 : public function render()
92 : {
93 : //var_dump($this->getMarkers());die;
94 :
95 : // Create a div element to hold the Map.
96 0 : echo "<div id=\"".$this->getId()."\" style=\"width:".$this->options->get('width')."; height:".$this->options->get('height')."\"></div>\n";
97 :
98 : // GoogleMaps won't work without at least one location.
99 0 : if (!$this->hasMarkers()) {
100 0 : return;
101 : }
102 : else {
103 0 : $markers = $this->getMarkers();
104 : }
105 :
106 : echo
107 : "<script type=\"text/javascript\">\n".
108 0 : "function showmap() {\n".
109 0 : " var markersArray = [];\n".
110 0 : " var bounds = new google.maps.LatLngBounds();\n".
111 0 : " var infowindowsArray = [];\n".
112 0 : "\n";
113 :
114 : // Create the map object
115 0 : $kc = $this->options->get('center');
116 : echo
117 : " var mapOptions = {\n".
118 0 : " zoom: ".$this->options->get('zoom').",\n".
119 0 : " center: new google.maps.LatLng(".$markers[$kc]->getLatitude().", ".$markers[$kc]->getLongitude()."),\n".
120 0 : " mapTypeId: google.maps.MapTypeId.".strtoupper($this->options->get('type'))."\n".
121 0 : " };\n".
122 0 : " var map = new google.maps.Map(document.getElementById(\"".$this->getId()."\"), mapOptions);\n".
123 0 : "\n";
124 :
125 : // Insert markers
126 0 : foreach (array_keys($markers) as $k) {
127 : echo
128 0 : " // Marker $k\n".
129 0 : " markersArray[$k] = new google.maps.Marker({\n".
130 0 : " position: new google.maps.LatLng(".$markers[$k]->getLatitude().", ".$markers[$k]->getLongitude()."),\n".
131 0 : " map: map";
132 0 : if ($markers[$k]->options->has('icon')) {
133 0 : echo ",\n icon: '".$markers[$k]->options->get('icon')."'";
134 0 : }
135 0 : if ($markers[$k]->options->has('title')) {
136 0 : echo ",\n title: '".$markers[$k]->options->get('title')."'";
137 0 : }
138 : echo
139 0 : "\n });\n";
140 : // Info windows
141 0 : if ($markers[$k]->options->has('infowindow')) {
142 : echo
143 0 : " infowindowsArray[$k] = new google.maps.InfoWindow({\n".
144 0 : " content: '".$markers[$k]->options->get('infowindow')."'\n".
145 0 : " });\n".
146 0 : " google.maps.event.addListener(markersArray[$k], 'click', function() {\n".
147 0 : " infowindowsArray[$k].open(map, markersArray[$k]);\n".
148 0 : " });\n";
149 0 : }
150 0 : echo " bounds.extend(markersArray[$k].getPosition());\n";
151 0 : }
152 : // Auto-center and auto-zoom
153 : echo
154 : " map.fitBounds(bounds);\n".
155 0 : " map.setCenter(bounds.getCenter());\n".
156 0 : "}\n".
157 0 : "window.onload = showmap;\n".
158 0 : "</script>\n";
159 0 : }
160 : }
161 :
|