Completed
Push — master ( ece61f...860305 )
by Mike
03:40
created

Map::add_ground_overlay()   C

Complexity

Conditions 10
Paths 51

Size

Total Lines 68
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 6.0995
c 0
b 0
f 0
cc 10
eloc 45
nc 51
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace GeneaLabs\LaravelMaps;
2
3
class Map
4
{
5
6
    protected $output_js;
7
    protected $output_js_contents;
8
    protected $output_html;
9
10
    public $adsense = false;                    // Whether Google Adsense For Content should be enabled
11
    public $adsenseChannelNumber = '';                        // The Adsense channel number for tracking the performance of this AdUnit
12
    public $adsenseFormat = 'HALF_BANNER';            // The format of the AdUnit
13
    public $adsensePosition = 'TOP_CENTER';                // The position of the AdUnit
14
    public $adsensePublisherID = '';                        // Your Google AdSense publisher ID
15
    public $apiKey = '';                        // If you've got an API key you can use it by passing this parameter. Setup an API key here: https://code.google.com/apis/console
16
    public $backgroundColor = '';                        // A hex color value shown as the map background when tiles have not yet loaded as the user pans
17
    public $bicyclingOverlay = false;                    // If set to TRUE will overlay bicycling information (ie. bike paths and suggested routes) onto the map by default
18
    public $center = '37.4419, -122.1419';        // Sets the default center location (lat/long co-ordinate or address) of the map. If defaulting to the users location set to "auto"
19
20
    public $class = '';                        // A class name if wishing to style the map further through CSS. Can also be useful if wanting it to be responsive etc.
21
    public $cluster = false;                    // Whether to cluster markers
22
    public $clusterGridSize = 60;                        // The grid size of a cluster in pixels
23
    public $clusterMaxZoom = '';                        // The maximum zoom level that a marker can be part of a cluster
24
    public $clusterZoomOnClick = true;                        // Whether the default behaviour of clicking on a cluster is to zoom into it
25
    public $clusterAverageCenter = false;                    // Whether the center of each cluster should be the average of all markers in the cluster
26
    public $clusterMinimumClusterSize = 2;                        // The minimum number of markers to be in a cluster before the markers are hidden and a count is shown
27
    public $clusterStyles = []; 				// (object) An array that has style properties: *  'url': (string) The image url. *  'height': (number) The image height. *  'width': (number) The image width. *  'anchor': (Array) The anchor position of the label text. *  'textColor': (string) The text color. *  'textSize': (number) The text size. *  'backgroundPosition': (string) The position of the backgound x, y.
28
    public $disableDefaultUI = false;                    // If set to TRUE will hide the default controls (ie. zoom, scale etc)
29
    public $disableDoubleClickZoom = false;                    // If set to TRUE will disable zooming when a double click occurs
30
    public $disableMapTypeControl = false;                    // If set to TRUE will hide the MapType control (ie. Map, Satellite, Hybrid, Terrain)
31
    public $disableNavigationControl = false;                    // If set to TRUE will hide the Navigation control (ie. zoom in/out, pan)
32
    public $disableScaleControl = false;                    // If set to TRUE will hide the Scale control
33
    public $disableStreetViewControl = false;                    // If set to TRUE will hide the Street View control
34
    public $draggable = true;                        // If set to FALSE will prevent the map from being dragged around
35
    public $draggableCursor = '';                        // The name or url of the cursor to display on a draggable object
36
    public $draggingCursor = '';                        // The name or url of the cursor to display when an object is being dragged
37
    public $geocodeCaching = false;                    // If set to TRUE will cache any geocode requests made when an address is used instead of a lat/long. Requires DB table to be created (see documentation)
38
    public $https = false;                    // If set to TRUE will load the Google Maps JavaScript API over HTTPS, allowing you to utilize the API within your HTTPS secure application
39
    public $navigationControlPosition = '';                        // The position of the Navigation control, eg. 'BOTTOM_RIGHT'
40
    public $infowindowMaxWidth = 0;                        // The maximum width of the infowindow in pixels. Expecting an integer without units
41
    public $keyboardShortcuts = true;                        // If set to FALSE will disable to map being controlled via the keyboard
42
    public $jsfile = '';                        // Set this to the path of an external JS file if you wish the JavaScript to be placed in a file rather than output directly into the <head></head> section. The library will try to create the file if it does not exist already. Please ensure the destination file is writeable
43
    public $kmlLayerURL = '';                        // A URL to publicly available KML or GeoRSS data for displaying geographic information. Multiple KML layers can be passed in by using an array of URL's. Note, if using multiple you'll probably have to set $kmlLayerPreserveViewport to true and manually set map center and zoom
44
    public $kmlLayerPreserveViewport = false;                    // Specifies whether the map should be adjusted to the bounds of the KmlLayer's contents. By default the map is zoomed and positioned to show the entirety of the layer's contents
45
    public $language = '';                        // The map will by default load in the language of the browser. This can be overriden however here. For a full list of codes see https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1
46
    public $loadAsynchronously = false;                    // Load the map and API asynchronously once the page has loaded
47
    public $map_div_id = "map_canvas";                // The ID of the <div></div> that is output which contains the map
48
    public $map_height = "450px";                    // The height of the map container. Any units (ie 'px') can be used. If no units are provided 'px' will be presumed
49
    public $map_name = "map";                    // The JS reference to the map. Currently not used but to be used in the future when multiple maps are supported
50
    public $map_type = "ROADMAP";                // The default MapType. Values accepted are 'HYBRID', 'ROADMAP', 'SATELLITE' or 'TERRAIN'
51
    public $map_types_available = array();                    // The other MapTypes available for selection on the map
52
    public $map_width = "100%";                    // The width of the map container. Any units (ie 'px') can be used. If no units are provided 'px' will be presumed
53
    public $maps_loaded = 0;                        // Counter which keeps track of how many maps have been created to avoid standard functions being output twice
54
    public $mapTypeControlPosition = '';                        // The position of the MapType control, eg. 'BOTTOM_RIGHT'
55
    public $mapTypeControlStyle = '';                        // The style of the MapType control. blank, 'DROPDOWN_MENU' or 'HORIZONTAL_BAR' values accepted.
56
    public $minzoom = '';                        // The minimum zoom level which will be displayed on the map
57
    public $maxzoom = '';                        // The maximum zoom level which will be displayed on the map
58
    public $minifyJS = false;                    // If TRUE will run the JavaScript through Jsmin.php (this file and PHP5+ required) to minify the code
59
    public $noClear = false;                    // If TRUE do not clear the contents of the map div
60
    public $onboundschanged = '';                        // The JavaScript action to perform when the viewport bounds have changed
61
    public $oncenterchanged = '';                        // The JavaScript action to perform when themap center property changes
62
    public $onclick = '';                        // The JavaScript action to perform when the map is clicked
63
    public $ondblclick = '';                        // The JavaScript action to perform when the map is double-clicked
64
    public $ondrag = '';                        // The JavaScript action to perform while the map is dragged
65
    public $ondragend = '';                        // The JavaScript action to perform when the user stops dragging the map
66
    public $ondragstart = '';                        // The JavaScript action to perform when the user starts dragging the map
67
    public $onidle = '';                        // The JavaScript action to perform when the map becomes idle after panning or zooming
68
    public $onload = '';                        // The JavaScript action to perform when the map first loads. This library hi-jacks the window.load event so add any bespoke code using this option
69
    public $onmousemove = '';                        // The JavaScript action to perform when the user's mouse moves over the map container
70
    public $onmouseout = '';                        // The JavaScript action to perform when the user's mouse exits the map container
71
    public $onmouseover = '';                        // The JavaScript action to perform when the user's mouse enters the map container
72
    public $onresize = '';                        // The JavaScript action to perform when the maps div changes size
73
    public $onrightclick = '';                        // The JavaScript action to perform when the map is right-clicked
74
    public $ontilesloaded = '';                        // The JavaScript action to perform when the visible tiles have finished loading
75
    public $onzoomchanged = '';                        // The JavaScript action to perform when the maps zoom property changes
76
    public $panoramio = false;                    // If TRUE will add photos from Panoramio as a layer to your maps as a series of large and small photo icons
77
    public $panoramioTag = '';                        // Restrict the set of Panoramio photos shown to those matching a certain textual tag
78
    public $panoramioUser = '';                        // Restrict the set of Panoramio photos shown to those matching a particular user
79
    public $region = '';                        // Country code top-level domain (eg "uk") within which to search. Useful if supplying addresses rather than lat/longs
80
    public $scaleControlPosition = '';                        // The position of the Scale control, eg. 'BOTTOM_RIGHT'
81
    public $scrollwheel = true;                        // If set to FALSE will disable zooming by scrolling of the mouse wheel
82
    public $streetViewAddressControl = true;                        // If set to FALSE will hide the Address control
83
    public $streetViewAddressPosition = '';                        // The position of the Address control, eg. 'BOTTOM'
84
    public $streetViewControlPosition = '';                        // The position of the Street View control when viewing normal aerial map, eg. 'BOTTOM_RIGHT'
85
    public $streetViewCloseButton = false;                    // If set to TRUE will show the close button in the top right. The close button allows users to return to the aerial map
86
    public $streetViewLinksControl = true;                        // If set to FALSE will hide the Links control
87
    public $streetViewPanControl = true;                        // If set to FALSE will hide the Pan control
88
    public $streetViewPanPosition = '';                        // The position of the Scale control, eg. 'TOP_RIGHT'
89
    public $streetViewPovHeading = 0;                        // The Street View camera heading in degrees relative to true north. True north is 0, east is 90, south is 180, west is 270
90
    public $streetViewPovPitch = 0;                        // The Street View camera pitch in degrees, relative to the street view vehicle. Directly upwards is 90, Directly downwards is -90.
91
    public $streetViewPovZoom = 0;                        // The Street View zoom level. Fully zoomed-out is level 0, zooming in increases the zoom level.
92
    public $streetViewZoomControl = true;                        // If set to FALSE will hide the Zoom control
93
    public $streetViewZoomPosition = '';                        // The position of the Scale control, eg. 'TOP_RIGHT'
94
    public $streetViewZoomStyle = '';                        // The size of the Street View zoom control. blank, 'SMALL' or 'LARGE' values accepted.
95
    public $styles = array();                    // An array of styles used to colour aspects of the map and turn points of interest on and off
96
    public $stylesAsMapTypes = false;                    // If applying styles, whether to apply them to the default map or add them as additional map types
97
    public $stylesAsMapTypesDefault = '';                        // If $stylesAsMapTypes is true the default style. Should contain the 'Name' of the style
98
    public $tiledOverlayLayer = "";
99
    public $tilt = 0;                        // The angle of tilt. Currently only supports the values 0 and 45 in SATELLITE and HYBRID map types and at certain zoom levels
100
    public $trafficOverlay = false;                    // If set to TRUE will overlay traffic information onto the map by default
101
    public $version = "3";                        // Version of the API being used. Not currently used in the library
102
    public $zoom = 13;                        // The default zoom level of the map. If set to "auto" will autozoom/center to fit in all visible markers. If "auto", also overrides the $center parameter
103
    public $zoomControlPosition = '';                        // The position of the Zoom control, eg. 'BOTTOM_RIGHT'
104
    public $zoomControlStyle = '';                        // The size of the zoom control. blank, 'SMALL' or 'LARGE' values accepted.
105
106
    public $markers = array();                    // An array used by the library to store the markers as they are produced
107
    public $markersInfo = array();                    // An array containing marker information (id, latitude, longitude etc) for use elsewhere
108
    public $polylines = array();                    // An array used by the library to store the polylines as they are produced
109
    public $polygons = array();                    // An array used by the library to store the polygons as they are produced
110
    public $circles = array();                    // An array used by the library to store the circles as they are produced
111
    public $rectangles = array();                    // An array used by the library to store the rectangles as they are produced
112
    public $overlays = array();                    // An array used by the library to store the overlays as they are produced
113
114
    public $directions = false;                    // Whether or not the map will be used to show directions
115
    public $directionsStart = "";                        // The starting location (lat/long co-ordinate or address) of the directions. Set to 'auto' to default it to the users location
116
    public $directionsEnd = "";                        // The destination point (lat/long co-ordinate or address) of the directions. Set to 'auto' to default it to the users location
117
    public $directionsDivID = "";                        // An element's ID on the page where textual directions will be output to. Leave blank if not required
118
    public $directionsMode = "DRIVING";                // DRIVING, WALKING or BICYCLING (US Only) - The vehicle/mode of transport to show directions for
119
    public $directionsAvoidTolls = false;                    // Whether or not directions should avoid tolls
120
    public $directionsAvoidHighways = false;                    // Whether or not directions should avoid highways
121
    public $directionsDraggable = false;                    // Whether or not directions on the map are draggable
122
    public $directionsChanged = "";                        // JavaScript to perform when directions are dragged
123
    public $directionsUnits = "";                        // 'metric' for kilometers and meters or 'imperial for miles and feet. Leave blank and it will default to the region or country of where directions are being obtained
124
    public $directionsWaypointArray = array();                    // An array of waypoints. eg array("Boston, MA", "Times Square, NY");
125
    public $directionsWaypointsOptimize = false;                    // Should the waypoints be optimised? If TRUE, waypoints will be re-ordered to provide the most efficient route.
126
127
    public $drawing = false;                    // Whether or not the drawing library tools will be loaded
128
    public $drawingControl = true;                        // If set to FALSE will hide the Drawing Manager control
129
    public $drawingControlPosition = 'TOP_CENTER';                // The position of the Drawing Manager control, eg. 'TOP_RIGHT'
130
    public $drawingDefaultMode = 'marker';                    // The default mode for the Drawing Manager. Accepted values are marker, polygon, polyline, rectangle, circle, or null. null means that the user can interact with the map as normal when the map loads, and clicks do not draw anything.
131
    public $drawingModes = array();                    // An array of modes available for use. Accepted values are marker, polygon, polyline, rectangle, circle
132
    public $drawingOnComplete = array();                    // An array of JS to execute when shapes are completed, one array element per shape. For example: array('circle'=>'JS here', 'polygon'=>'JS here');
133
    public $drawingOnEdit = array();                    // An array of JS to execute when shapes are changed/resized, one array element per shape. For example: array('circle'=>'JS here', 'polygon'=>'JS here');
134
135
    public $places = false;                    // Whether or not the map will be used to show places
136
    public $placesLocation = '';                        // A point (lat/long co-ordinate or address) on the map if the search for places is based around a central point
137
    public $placesRadius = 0;                        // The radius (in meters) if search is based around a central position
138
    public $placesLocationSW = '';                        // If preferring to search within bounds the South-West position (latitude/longitude coordinate OR address)
139
    public $placesLocationNE = '';                        // If preferring to search within bounds the North-East position (latitude/longitude coordinate OR address)
140
    public $placesTypes = array();                    // The types of places to search for. For a list of supported types see http://code.google.com/apis/maps/documentation/places/supported_types.html
141
    public $placesName = '';                        // A term to be matched against when searching for places to display on the map
142
    public $placesAutocompleteInputID = '';                        // The ID attribute of the textfield that the autocomplete should effect
143
    public $placesAutocompleteTypes = array();                    // The types of places for the autocomplete to return. Options can be seen here https://developers.google.com/maps/documentation/javascript/places#places_autocomplete but include 'establishment' to only return business results, '(cities)', or '(regions)'
144
    public $placesAutocompleteBoundSW = '';                        // By specifying an area in which to search for Places, the results are biased towards, but not restricted to, Places contained within these bounds.
145
    public $placesAutocompleteBoundNE = '';                        // Both South-West (lat/long co-ordinate or address) and North-East (lat/long co-ordinate or address) values are required if wishing to set bounds
146
    public $placesAutocompleteBoundsMap = false;                    // An alternative to setting the SW and NE bounds is to use the bounds of the current viewport. If set to TRUE, the bounds will be set to the viewport of the visible map, even if dragged or zoomed
147
    public $placesAutocompleteOnChange = '';                        // The JavaScript action to perform when a place is selected
148
    public $gestureHandling = 'auto';                                // Controls the panning and scrolling behavior of a map when viewed on a mobile device. greedy(allways moves on touch), cooperative(1 finger scroll 2 finger move), none(not pannable or pinchable), auto
149
150
151
    public function __construct($config = array())
152
    {
153
        if (count($config) > 0) {
154
            $this->initialize($config);
155
        }
156
157
        #Log::info('debug', "Google Maps Class Initialized");
158
    }
159
160
    public function create($center)
161
    {
162
        $gmapsConfig = [];
163
        $gmapsConfig['center'] = $center->latitude . ', ' . $center->longitude;
164
        $gmapsConfig['onboundschanged'] = "if ((typeof centreGot === 'undefined') || !centreGot) {
165
            var mapCentre = map.getCenter();
166
            marker_0.setOptions({
167
                position: new google.maps.LatLng(mapCentre.lat(), mapCentre.lng())
168
            });
169
        }
170
        centreGot = true;";
171
        $this->initialize($gmapsConfig);
172
        $gmapsMarker = [];
173
        $gmapsMarker['position'] = $center->latitude . ', ' . $center->longitude;
174
        $gmapsMarker['animation'] = 'DROP';
175
        $gmapsMarker['highlightBackgroundColor'] = 'ff0000';
176
        $this->add_marker($gmapsMarker);
177
178
        return $this->create_map();
179
    }
180
181
    public function initialize($config = array())
182
    {
183
        foreach ($config as $key => $val) {
184
            if (isset($this->$key)) {
185
                $this->$key = $val;
186
            }
187
        }
188
    }
189
190
    public function addOverlayLayer(string $tileOverlayFolderUrl)
191
    {
192
        if (! $tileOverlayFolderUrl) {
193
            $this->tiledOverlayLayer = "";
194
195
            return;
196
        }
197
198
        $this->tiledOverlayLayer = "var maptiler = new google.maps.ImageMapType({
199
            getTileUrl: function(coord, zoom) {
200
                var mapBounds = map.getBounds();
201
                var mapMinZoom = 9;
202
                var mapMaxZoom = 14;
203
                var proj = map.getProjection();
204
                var z2 = Math.pow(2, zoom);
205
                var tileXSize = 256 / z2;
206
                var tileYSize = 256 / z2;
207
                var tileBounds = new google.maps.LatLngBounds(
208
                    proj.fromPointToLatLng(new google.maps.Point(coord.x * tileXSize, (coord.y + 1) * tileYSize)),
209
                    proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileXSize, coord.y * tileYSize))
210
                );
211
                var y = coord.y;
212
                var x = coord.x >= 0 ? coord.x : z2 + coord.x
213
                if (mapBounds.intersects(tileBounds)) {
214
                    return \"{$tileOverlayFolderUrl}/\" + zoom + \"/\" + x + \"/\" + y + \".png\";
215
                } else {
216
                    return \"https://www.maptiler.com/img/none.png\";
217
                }
218
            },
219
            tileSize: new google.maps.Size(256, 256),
220
            isPng: true,
221
            opacity: 1.0
222
        });";
223
224
        return;
225
    }
226
227
    public function add_marker($params = array())
228
    {
229
        $marker = array();
230
        $this->markersInfo['marker_'.count($this->markers)] = array();
231
232
        $marker['position'] = '';                                // The position (lat/long co-ordinate or address) at which the marker will appear
233
        $marker['infowindow_content'] = '';                        // If not blank, creates an infowindow (aka bubble) with the content provided. Can be plain text or HTML
234
        $marker['id'] = '';                                        // The unique identifier of the marker suffix (ie. marker_yourID). If blank, this will default to marker_X where X is an incremental number
235
        $marker['clickable'] = true;                            // Defines if the marker is clickable
236
        $marker['cursor'] = '';                                    // The name or url of the cursor to display on hover
237
        $marker['draggable'] = false;                            // Defines if the marker is draggable
238
        $marker['flat'] = false;                                // If set to TRUE will not display a shadow beneath the icon
239
        $marker['icon'] = '';                                    // The name or url of the icon to use for the marker
240
        $marker['icon_size'] = '';                                // The display size of the sprite or image being used. When using sprites, you must specify the sprite size. Expecting two comma-separated values for width and height respectively (ie '20,30'). See https://developers.google.com/maps/documentation/javascript/3.exp/reference#Icon
241
        $marker['icon_scaledSize'] = '';                        // The size of the entire image after scaling, if any. Use this property to stretch/shrink an image or a sprite. Expecting two comma-separated values for width and height respectively (ie '20,30')
242
        $marker['icon_origin'] = '';                            // If using a sprite, the position of the image within the sprite. Expecting two comma-separated values for distance from the top and left respectively (ie '20,30')
243
        $marker['icon_anchor'] = '';                            // The position at which to anchor an image in correspondance to the location of the marker on the map. By default, the anchor is located along the center point of the bottom of the image. Expecting two comma-separated values (ie '20,30'). Credit to https://github.com/colethorsen
244
        $marker['animation'] = '';                                // blank, 'DROP' or 'BOUNCE'
245
        $marker['onclick'] = '';                                // JavaScript performed when a marker is clicked
246
        $marker['ondblclick'] = '';                                // JavaScript performed when a marker is double-clicked
247
        $marker['ondrag'] = '';                                    // JavaScript repeatedly performed while the marker is being dragged
248
        $marker['ondragstart'] = '';                            // JavaScript performed when a marker is started to be dragged
249
        $marker['ondragend'] = '';                                // JavaScript performed when a draggable marker is dropped
250
        $marker['onmousedown'] = '';                            // JavaScript performed when a mousedown event occurs on a marker
251
        $marker['onmouseout'] = '';                                // JavaScript performed when the mouse leaves the area of the marker icon
252
        $marker['onmouseover'] = '';                            // JavaScript performed when the mouse enters the area of the marker icon
253
        $marker['onmouseup'] = '';                                // JavaScript performed when a mouseup event occurs on a marker
254
        $marker['onpositionchanged'] = '';                        // JavaScript performed when the markers position changes
255
        $marker['onrightclick'] = '';                            // JavaScript performed when a right-click occurs on a marker
256
        $marker['raiseondrag'] = true;                            // If FALSE, disables the raising and lowering of the icon when a marker is being dragged
257
        $marker['shadow'] = '';                                    // The name or url of the icon's shadow
258
        $marker['title'] = '';                                    // The tooltip text to show on hover
259
        $marker['visible'] = true;                                // Defines if the marker is visible by default
260
        $marker['zIndex'] = '';                                    // The zIndex of the marker. If two markers overlap, the marker with the higher zIndex will appear on top
261
        $marker['label'] = '';                                    // The label of the marker.
262
263
        $marker_output = '';
264
265
        foreach ($params as $key => $value) {
266
            if (isset($marker[$key])) {
267
                $marker[$key] = $value;
268
            }
269
        }
270
271
        $marker_id = count($this->markers);
272
        if (trim($marker['id']) != "") {
273
            $marker_id = $marker['id'];
274
        }
275
276
        if ($marker['position'] != "") {
277
            if ($this->is_lat_long($marker['position'])) {
278
                $marker_output .= '
279
			var myLatlng = new google.maps.LatLng('.$marker['position'].');
280
			';
281
                $explodePosition = explode(",", $marker['position']);
282
                $this->markersInfo['marker_'.$marker_id]['latitude'] = trim($explodePosition[0]);
283
                $this->markersInfo['marker_'.$marker_id]['longitude'] = trim($explodePosition[1]);
284
            } else {
285
                $lat_long = $this->get_lat_long_from_address($marker['position']);
286
                $marker_output .= '
287
			var myLatlng = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].');';
288
                $this->markersInfo['marker_'.$marker_id]['latitude'] = $lat_long[0];
289
                $this->markersInfo['marker_'.$marker_id]['longitude'] = $lat_long[1];
290
            }
291
        }
292
293
        if ($marker['icon'] != "") {
294
            $marker_output .= '
295
				var marker_icon = {
296
					url: "'.$marker['icon'].'"';
297
            if ($marker['icon_size'] != "") {
298
                $marker_output .= ',
299
					size: new google.maps.Size('.$marker['icon_size'].')';
300
            }
301
            if ($marker['icon_scaledSize'] != "") {
302
                $marker_output .= ',
303
					scaledSize: new google.maps.Size('.$marker['icon_scaledSize'].')';
304
            }
305
            if ($marker['icon_origin'] != "") {
306
                $marker_output .= ',
307
					origin: new google.maps.Point('.$marker['icon_origin'].')';
308
            }
309
            if ($marker['icon_anchor'] != "") {
310
                $marker_output .= ',
311
					anchor: new google.maps.Point('.$marker['icon_anchor'].')';
312
            }
313
            $marker_output .= '};
314
			';
315
        }
316
317
        $marker_output .= '
318
			var markerOptions = {
319
				map: '.$this->map_name;
320
        if ($marker['position'] != "") {
321
            $marker_output .= ',
322
				position: myLatlng';
323
        }
324
        if (!$marker['clickable']) {
325
            $marker_output .= ',
326
				clickable: false';
327
        }
328
        if ($marker['cursor'] != "") {
329
            $marker_output .= ',
330
				cursor: "'.$marker['cursor'].'"';
331
        }
332
        if ($marker['draggable']) {
333
            $marker_output .= ',
334
				draggable: true';
335
        }
336
        if ($marker['flat']) {
337
            $marker_output .= ',
338
				flat: true';
339
        }
340
        if ($marker['icon'] != "") {
341
            $marker_output .= ',
342
				icon: marker_icon';
343
        }
344
        if (!$marker['raiseondrag']) {
345
            $marker_output .= ',
346
				raiseOnDrag: false';
347
        }
348
        if ($marker['shadow'] != "") {
349
            $marker_output .= ',
350
				shadow: "'.$marker['shadow'].'"';
351
        }
352
        if ($marker['title'] != "") {
353
            $marker_output .= ',
354
				title: "'.$marker['title'].'"';
355
            $this->markersInfo['marker_'.$marker_id]['title'] = $marker['title'];
356
        }
357
        if (!$marker['visible']) {
358
            $marker_output .= ',
359
				visible: false';
360
        }
361
        if ($marker['zIndex'] != "" && is_numeric($marker['zIndex'])) {
362
            $marker_output .= ',
363
				zIndex: '.$marker['zIndex'];
364
        }
365
        if ($marker['animation'] != "" && (strtoupper($marker['animation']) == "DROP" || strtoupper($marker['animation'] == "BOUNCE"))) {
366
            $marker_output .= ',
367
				animation:  google.maps.Animation.'.strtoupper($marker['animation']);
368
        }
369
        if ($marker['label'] != "") {
370
            $marker_output .= ',
371
				label: "'.$marker['label'].'"';
372
        }
373
374
375
376
        $marker_output .= '
377
			};
378
			marker_'.$marker_id.' = createMarker_'.$this->map_name.'(markerOptions);
379
			';
380
381
        if ($marker['infowindow_content'] != "") {
382
383
            // Escape any quotes in the event that HTML is being added to the infowindow
384
            $marker['infowindow_content'] = str_replace('\"', '"', $marker['infowindow_content']);
385
            $marker['infowindow_content'] = str_replace('"', '\"', $marker['infowindow_content']);
386
387
            $marker_output .= '
388
			marker_'.$marker_id.'.set("content", "'.$marker['infowindow_content'].'");
389
390
			google.maps.event.addListener(marker_'.$marker_id.', "click", function(event) {
391
				iw_'.$this->map_name.'.setContent(this.get("content"));
392
				iw_'.$this->map_name.'.open('.$this->map_name.', this);
393
			';
394
            if ($marker['onclick'] != "") {
395
                $marker_output .= $marker['onclick'].'
396
			';
397
            }
398
            $marker_output .= '
399
			});
400
			';
401
        } else {
402
            if ($marker['onclick'] != "") {
403
                $marker_output .= '
404
				google.maps.event.addListener(marker_'.$marker_id.', "click", function(event) {
405
					'.$marker['onclick'].'
406
				});
407
				';
408
            }
409
        }
410
411
        if ($marker['ondblclick'] != "") {
412
            $marker_output .= '
413
			google.maps.event.addListener(marker_'.$marker_id.', "dblclick", function(event) {
414
				'.$marker['ondblclick'].'
415
			});
416
			';
417
        }
418
        if ($marker['onmousedown'] != "") {
419
            $marker_output .= '
420
			google.maps.event.addListener(marker_'.$marker_id.', "mousedown", function(event) {
421
				'.$marker['onmousedown'].'
422
			});
423
			';
424
        }
425
        if ($marker['onmouseout'] != "") {
426
            $marker_output .= '
427
			google.maps.event.addListener(marker_'.$marker_id.', "mouseout", function(event) {
428
				'.$marker['onmouseout'].'
429
			});
430
			';
431
        }
432
        if ($marker['onmouseover'] != "") {
433
            $marker_output .= '
434
			google.maps.event.addListener(marker_'.$marker_id.', "mouseover", function(event) {
435
				'.$marker['onmouseover'].'
436
			});
437
			';
438
        }
439
        if ($marker['onmouseup'] != "") {
440
            $marker_output .= '
441
			google.maps.event.addListener(marker_'.$marker_id.', "mouseup", function(event) {
442
				'.$marker['onmouseup'].'
443
			});
444
			';
445
        }
446
        if ($marker['onpositionchanged'] != "") {
447
            $marker_output .= '
448
			google.maps.event.addListener(marker_'.$marker_id.', "position_changed", function(event) {
449
				'.$marker['onpositionchanged'].'
450
			});
451
			';
452
        }
453
        if ($marker['onrightclick'] != "") {
454
            $marker_output .= '
455
			google.maps.event.addListener(marker_'.$marker_id.', "rightclick", function(event) {
456
				'.$marker['onrightclick'].'
457
			});
458
			';
459
        }
460
461
        if ($marker['draggable']) {
462
            if ($marker['ondrag'] != "") {
463
                $marker_output .= '
464
				google.maps.event.addListener(marker_'.$marker_id.', "drag", function(event) {
465
					'.$marker['ondrag'].'
466
				});
467
				';
468
            }
469
            if ($marker['ondragend'] != "") {
470
                $marker_output .= '
471
				google.maps.event.addListener(marker_'.$marker_id.', "dragend", function(event) {
472
					'.$marker['ondragend'].'
473
				});
474
				';
475
            }
476
            if ($marker['ondragstart'] != "") {
477
                $marker_output .= '
478
				google.maps.event.addListener(marker_'.$marker_id.', "dragstart", function(event) {
479
					'.$marker['ondragstart'].'
480
				});
481
				';
482
            }
483
        }
484
485
        array_push($this->markers, $marker_output);
486
    }
487
488
    public function add_polyline($params = array())
489
    {
490
        $polyline = array();
491
492
        $polyline['points'] = array();                            // An array of latitude/longitude coordinates OR addresses, or a mixture of both. If an address is supplied the Google geocoding service will be used to return a lat/long.
493
        $polyline['clickable'] = true;                            // Defines if the polyline is clickable
494
        $polyline['strokeColor'] = '#FF0000';                    // The hex value of the polylines color
495
        $polyline['strokeOpacity'] = '1.0';                        // The opacity of the polyline. 0 to 1.0
496
        $polyline['strokeWeight'] = '2';                        // The thickness of the polyline
497
        $polyline['onclick'] = '';                                // JavaScript performed when a polyline is clicked
498
        $polyline['ondblclick'] = '';                            // JavaScript performed when a polyline is double-clicked
499
        $polyline['onmousedown'] = '';                            // JavaScript performed when a mousedown event occurs on a polyline
500
        $polyline['onmousemove'] = '';                            // JavaScript performed when the mouse moves in the area of the polyline
501
        $polyline['onmouseout'] = '';                            // JavaScript performed when the mouse leaves the area of the polyline
502
        $polyline['onmouseover'] = '';                            // JavaScript performed when the mouse enters the area of the polyline
503
        $polyline['onmouseup'] = '';                            // JavaScript performed when a mouseup event occurs on a polyline
504
        $polyline['onrightclick'] = '';                            // JavaScript performed when a right-click occurs on a polyline
505
        $polyline['zIndex'] = '';                                // The zIndex of the polyline. If two polylines overlap, the polyline with the higher zIndex will appear on top
506
507
        $polyline_output = '';
508
509
        foreach ($params as $key => $value) {
510
            if (isset($polyline[$key])) {
511
                $polyline[$key] = $value;
512
            }
513
        }
514
515
        if (count($polyline['points'])) {
516
            $polyline_output .= '
517
				var polyline_plan_'.count($this->polylines).' = [';
518
            $i = 0;
519
            $lat_long_output = '';
520
            foreach ($polyline['points'] as $point) {
521
                if ($i > 0) {
522
                    $polyline_output .= ',';
523
                }
524
                $lat_long_to_push = '';
525
                if ($this->is_lat_long($point)) {
526
                    $lat_long_to_push = $point;
527
                    $polyline_output .= '
528
					new google.maps.LatLng('.$point.')
529
					';
530
                } else {
531
                    $lat_long = $this->get_lat_long_from_address($point);
532
                    $polyline_output .= '
533
					new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
534
                    $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
535
                }
536
                $lat_long_output .= '
537
					lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
538
				';
539
                $i++;
540
            }
541
            $polyline_output .= '];';
542
543
            $polyline_output .= $lat_long_output;
544
545
            $polyline_output .= '
546
				var polyline_'.count($this->polylines).' = new google.maps.Polyline({
547
    				path: polyline_plan_'.count($this->polylines).',
548
    				strokeColor: "'.$polyline['strokeColor'].'",
549
    				strokeOpacity: '.$polyline['strokeOpacity'].',
550
    				strokeWeight: '.$polyline['strokeWeight'];
551
            if (!$polyline['clickable']) {
552
                $polyline_output .= ',
553
					clickable: false';
554
            }
555
            if ($polyline['zIndex'] != "" && is_numeric($polyline['zIndex'])) {
556
                $polyline_output .= ',
557
					zIndex: '.$polyline['zIndex'];
558
            }
559
            $polyline_output .= '
560
				});
561
562
				polyline_'.count($this->polylines).'.setMap('.$this->map_name.');
563
564
			';
565
566
            if ($polyline['onclick'] != "") {
567
                $polyline_output .= '
568
				google.maps.event.addListener(polyline_'.count($this->polylines).', "click", function() {
569
					'.$polyline['onclick'].'
570
				});
571
				';
572
            }
573
            if ($polyline['ondblclick'] != "") {
574
                $polyline_output .= '
575
				google.maps.event.addListener(polyline_'.count($this->polylines).', "dblclick", function() {
576
					'.$polyline['ondblclick'].'
577
				});
578
				';
579
            }
580
            if ($polyline['onmousedown'] != "") {
581
                $polyline_output .= '
582
				google.maps.event.addListener(polyline_'.count($this->polylines).', "mousedown", function() {
583
					'.$polyline['onmousedown'].'
584
				});
585
				';
586
            }
587
            if ($polyline['onmousemove'] != "") {
588
                $polyline_output .= '
589
				google.maps.event.addListener(polyline_'.count($this->polylines).', "mousemove", function() {
590
					'.$polyline['onmousemove'].'
591
				});
592
				';
593
            }
594
            if ($polyline['onmouseout'] != "") {
595
                $polyline_output .= '
596
				google.maps.event.addListener(polyline_'.count($this->polylines).', "mouseout", function() {
597
					'.$polyline['onmouseout'].'
598
				});
599
				';
600
            }
601
            if ($polyline['onmouseover'] != "") {
602
                $polyline_output .= '
603
				google.maps.event.addListener(polyline_'.count($this->polylines).', "mouseover", function() {
604
					'.$polyline['onmouseover'].'
605
				});
606
				';
607
            }
608
            if ($polyline['onmouseup'] != "") {
609
                $polyline_output .= '
610
				google.maps.event.addListener(polyline_'.count($this->polylines).', "mouseup", function() {
611
					'.$polyline['onmouseup'].'
612
				});
613
				';
614
            }
615
            if ($polyline['onrightclick'] != "") {
616
                $polyline_output .= '
617
				google.maps.event.addListener(polyline_'.count($this->polylines).', "rightclick", function() {
618
					'.$polyline['onrightclick'].'
619
				});
620
				';
621
            }
622
623
            array_push($this->polylines, $polyline_output);
624
        }
625
    }
626
627
    public function add_polygon($params = array())
628
    {
629
        $polygon = array();
630
631
        $polygon['points'] = array();                            // The positions (latitude/longitude coordinates OR addresses) at which the polygon points will appear. NOTE: The first and last elements of the array must be the same
632
        $polygon['clickable'] = true;                            // Defines if the polygon is clickable
633
        $polygon['strokeColor'] = '#FF0000';                    // The hex value of the polygons border color
634
        $polygon['strokeOpacity'] = '0.8';                        // The opacity of the polygon border. 0 to 1.0
635
        $polygon['strokeWeight'] = '2';                            // The thickness of the polygon border
636
        $polygon['fillColor'] = '#FF0000';                        // The hex value of the polygons fill color
637
        $polygon['fillOpacity'] = '0.3';                        // The opacity of the polygons fill
638
        $polygon['onclick'] = '';                                // JavaScript performed when a polygon is clicked
639
        $polygon['ondblclick'] = '';                            // JavaScript performed when a polygon is double-clicked
640
        $polygon['onmousedown'] = '';                            // JavaScript performed when a mousedown event occurs on a polygon
641
        $polygon['onmousemove'] = '';                            // JavaScript performed when the mouse moves in the area of the polygon
642
        $polygon['onmouseout'] = '';                            // JavaScript performed when the mouse leaves the area of the polygon
643
        $polygon['onmouseover'] = '';                            // JavaScript performed when the mouse enters the area of the polygon
644
        $polygon['onmouseup'] = '';                                // JavaScript performed when a mouseup event occurs on a polygon
645
        $polygon['onrightclick'] = '';                            // JavaScript performed when a right-click occurs on a polygon
646
        $polygon['zIndex'] = '';                                // The zIndex of the polygon. If two polygons overlap, the polygon with the higher zIndex will appear on top
647
648
        $polygon_output = '';
649
650
        foreach ($params as $key => $value) {
651
            if (isset($polygon[$key])) {
652
                $polygon[$key] = $value;
653
            }
654
        }
655
656
        if (count($polygon['points'])) {
657
            $polygon_output .= '
658
				var polygon_plan_'.count($this->polygons).' = [';
659
            $i = 0;
660
            $lat_long_output = '';
661
            foreach ($polygon['points'] as $point) {
662
                if ($i > 0) {
663
                    $polygon_output .= ',';
664
                }
665
                $lat_long_to_push = '';
666
                if ($this->is_lat_long($point)) {
667
                    $lat_long_to_push = $point;
668
                    $polygon_output .= '
669
					new google.maps.LatLng('.$point.')
670
					';
671
                } else {
672
                    $lat_long = $this->get_lat_long_from_address($point);
673
                    $polygon_output .= '
674
					new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
675
                    $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
676
                }
677
                $lat_long_output .= '
678
					lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
679
				';
680
                $i++;
681
            }
682
            $polygon_output .= '];';
683
684
            $polygon_output .= $lat_long_output;
685
        }
686
687
        $polygon_output .= '
688
			var polygon_'.count($this->polygons).' = new google.maps.Polygon({
689
    			';
690
        if (count($polygon['points'])) {
691
            $polygon_output .= 'path: polygon_plan_'.count($this->polygons).',
692
					';
693
        }
694
        $polygon_output .= '
695
    			strokeColor: "'.$polygon['strokeColor'].'",
696
    			strokeOpacity: '.$polygon['strokeOpacity'].',
697
    			strokeWeight: '.$polygon['strokeWeight'].',
698
				fillColor: "'.$polygon['fillColor'].'",
699
				fillOpacity: '.$polygon['fillOpacity'];
700
        if (!$polygon['clickable']) {
701
            $polygon_output .= ',
702
				clickable: false';
703
        }
704
        if ($polygon['zIndex'] != "" && is_numeric($polygon['zIndex'])) {
705
            $polygon_output .= ',
706
				zIndex: '.$polygon['zIndex'];
707
        }
708
        $polygon_output .= '
709
			});
710
711
			polygon_'.count($this->polygons).'.setMap('.$this->map_name.');
712
713
		';
714
715
        if ($polygon['onclick'] != "") {
716
            $polygon_output .= '
717
			google.maps.event.addListener(polygon_'.count($this->polygons).', "click", function() {
718
				'.$polygon['onclick'].'
719
			});
720
			';
721
        }
722
        if ($polygon['ondblclick'] != "") {
723
            $polygon_output .= '
724
			google.maps.event.addListener(polygon_'.count($this->polygons).', "dblclick", function() {
725
				'.$polygon['ondblclick'].'
726
			});
727
			';
728
        }
729
        if ($polygon['onmousedown'] != "") {
730
            $polygon_output .= '
731
			google.maps.event.addListener(polygon_'.count($this->polygons).', "mousedown", function() {
732
				'.$polygon['onmousedown'].'
733
			});
734
			';
735
        }
736
        if ($polygon['onmousemove'] != "") {
737
            $polygon_output .= '
738
			google.maps.event.addListener(polygon_'.count($this->polygons).', "mousemove", function() {
739
				'.$polygon['onmousemove'].'
740
			});
741
			';
742
        }
743
        if ($polygon['onmouseout'] != "") {
744
            $polygon_output .= '
745
			google.maps.event.addListener(polygon_'.count($this->polygons).', "mouseout", function() {
746
				'.$polygon['onmouseout'].'
747
			});
748
			';
749
        }
750
        if ($polygon['onmouseover'] != "") {
751
            $polygon_output .= '
752
			google.maps.event.addListener(polygon_'.count($this->polygons).', "mouseover", function() {
753
				'.$polygon['onmouseover'].'
754
			});
755
			';
756
        }
757
        if ($polygon['onmouseup'] != "") {
758
            $polygon_output .= '
759
			google.maps.event.addListener(polygon_'.count($this->polygons).', "mouseup", function() {
760
				'.$polygon['onmouseup'].'
761
			});
762
			';
763
        }
764
        if ($polygon['onrightclick'] != "") {
765
            $polygon_output .= '
766
			google.maps.event.addListener(polygon_'.count($this->polygons).', "rightclick", function() {
767
				'.$polygon['onrightclick'].'
768
			});
769
			';
770
        }
771
772
        array_push($this->polygons, $polygon_output);
773
    }
774
775
    public function add_circle($params = array())
776
    {
777
        $circle = array();
778
779
        $circle['center'] = '';                                    // The center position (latitude/longitude coordinate OR addresse) at which the circle will appear
780
        $circle['clickable'] = true;                            // Defines if the circle is clickable
781
        $circle['radius'] = 0;                                    // The circle radius (in metres)
782
        $circle['strokeColor'] = '0.8';                            // The hex value of the circles border color
783
        $circle['strokeOpacity'] = '0.8';                        // The opacity of the circle border
784
        $circle['strokeWeight'] = '2';                            // The thickness of the circle border
785
        $circle['fillColor'] = '#FF0000';                        // The hex value of the circles fill color
786
        $circle['fillOpacity'] = '0.3';                            // The opacity of the circles fill
787
        $circle['onclick'] = '';                                // JavaScript performed when a circle is clicked
788
        $circle['ondblclick'] = '';                                // JavaScript performed when a circle is double-clicked
789
        $circle['onmousedown'] = '';                            // JavaScript performed when a mousedown event occurs on a circle
790
        $circle['onmousemove'] = '';                            // JavaScript performed when the mouse moves in the area of the circle
791
        $circle['onmouseout'] = '';                                // JavaScript performed when the mouse leaves the area of the circle
792
        $circle['onmouseover'] = '';                            // JavaScript performed when the mouse enters the area of the circle
793
        $circle['onmouseup'] = '';                                // JavaScript performed when a mouseup event occurs on a circle
794
        $circle['onrightclick'] = '';                            // JavaScript performed when a right-click occurs on a circle
795
        $circle['zIndex'] = '';                                    // The zIndex of the circle. If two circles overlap, the circle with the higher zIndex will appear on top
796
797
        $circle_output = '';
798
799
        foreach ($params as $key => $value) {
800
            if (isset($circle[$key])) {
801
                $circle[$key] = $value;
802
            }
803
        }
804
805
        if ($circle['radius'] > 0 && $circle['center'] != "") {
806
            $lat_long_to_push = '';
807
            if ($this->is_lat_long($circle['center'])) {
808
                $lat_long_to_push = $circle['center'];
809
                $circle_output = '
810
				var circleCenter = new google.maps.LatLng('.$circle['center'].')
811
				';
812
            } else {
813
                $lat_long = $this->get_lat_long_from_address($circle['center']);
814
                $circle_output = '
815
				var circleCenter = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
816
                $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
817
            }
818
            $circle_output .= '
819
				lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
820
			';
821
822
            $circle_output .= '
823
				var circleOptions = {
824
					strokeColor: "'.$circle['strokeColor'].'",
825
					strokeOpacity: '.$circle['strokeOpacity'].',
826
					strokeWeight: '.$circle['strokeWeight'].',
827
					fillColor: "'.$circle['fillColor'].'",
828
					fillOpacity: '.$circle['fillOpacity'].',
829
					map: '.$this->map_name.',
830
					center: circleCenter,
831
					radius: '.$circle['radius'];
832
            if (!$circle['clickable']) {
833
                $circle_output .= ',
834
					clickable: false';
835
            }
836
            if ($circle['zIndex'] != "" && is_numeric($circle['zIndex'])) {
837
                $circle_output .= ',
838
					zIndex: '.$circle['zIndex'];
839
            }
840
            $circle_output .= '
841
				};
842
				var circle_'.count($this->circles).' = new google.maps.Circle(circleOptions);
843
			';
844
845
            if ($circle['onclick'] != "") {
846
                $circle_output .= '
847
				google.maps.event.addListener(circle_'.count($this->circles).', "click", function() {
848
					'.$circle['onclick'].'
849
				});
850
				';
851
            }
852
            if ($circle['ondblclick'] != "") {
853
                $circle_output .= '
854
				google.maps.event.addListener(circle_'.count($this->circles).', "dblclick", function() {
855
					'.$circle['ondblclick'].'
856
				});
857
				';
858
            }
859
            if ($circle['onmousedown'] != "") {
860
                $circle_output .= '
861
				google.maps.event.addListener(circle_'.count($this->circles).', "mousedown", function() {
862
					'.$circle['onmousedown'].'
863
				});
864
				';
865
            }
866
            if ($circle['onmousemove'] != "") {
867
                $circle_output .= '
868
				google.maps.event.addListener(circle_'.count($this->circles).', "mousemove", function() {
869
					'.$circle['onmousemove'].'
870
				});
871
				';
872
            }
873
            if ($circle['onmouseout'] != "") {
874
                $circle_output .= '
875
				google.maps.event.addListener(circle_'.count($this->circles).', "mouseout", function() {
876
					'.$circle['onmouseout'].'
877
				});
878
				';
879
            }
880
            if ($circle['onmouseover'] != "") {
881
                $circle_output .= '
882
				google.maps.event.addListener(circle_'.count($this->circles).', "mouseover", function() {
883
					'.$circle['onmouseover'].'
884
				});
885
				';
886
            }
887
            if ($circle['onmouseup'] != "") {
888
                $circle_output .= '
889
				google.maps.event.addListener(circle_'.count($this->circles).', "mouseup", function() {
890
					'.$circle['onmouseup'].'
891
				});
892
				';
893
            }
894
            if ($circle['onrightclick'] != "") {
895
                $circle_output .= '
896
				google.maps.event.addListener(circle_'.count($this->circles).', "rightclick", function() {
897
					'.$circle['onrightclick'].'
898
				});
899
				';
900
            }
901
902
            array_push($this->circles, $circle_output);
903
        }
904
    }
905
906
    public function add_rectangle($params = array())
907
    {
908
        $rectangle = array();
909
910
        $rectangle['positionSW'] = '';                            // The South-West position (latitude/longitude coordinate OR address) at which the rectangle will appear
911
        $rectangle['positionNE'] = '';                            // The North-East position(latitude/longitude coordinate OR address) at which the rectangle will appear
912
        $rectangle['clickable'] = true;                            // Defines if the rectangle is clickable
913
        $rectangle['strokeColor'] = '0.8';                        // The hex value of the rectangles border color
914
        $rectangle['strokeOpacity'] = '0.8';                    // The opacity of the rectangle border
915
        $rectangle['strokeWeight'] = '2';                        // The thickness of the rectangle border
916
        $rectangle['fillColor'] = '#FF0000';                    // The hex value of the rectangles fill color
917
        $rectangle['fillOpacity'] = '0.3';                        // The opacity of the rectangles fill
918
        $rectangle['onclick'] = '';                                // JavaScript performed when a rectangle is clicked
919
        $rectangle['ondblclick'] = '';                            // JavaScript performed when a rectangle is double-clicked
920
        $rectangle['onmousedown'] = '';                            // JavaScript performed when a mousedown event occurs on a rectangle
921
        $rectangle['onmousemove'] = '';                            // JavaScript performed when the mouse moves in the area of the rectangle
922
        $rectangle['onmouseout'] = '';                            // JavaScript performed when the mouse leaves the area of the rectangle
923
        $rectangle['onmouseover'] = '';                            // JavaScript performed when the mouse enters the area of the rectangle
924
        $rectangle['onmouseup'] = '';                            // JavaScript performed when a mouseup event occurs on a rectangle
925
        $rectangle['onrightclick'] = '';                        // JavaScript performed when a right-click occurs on a rectangle
926
        $rectangle['zIndex'] = '';                                // The zIndex of the rectangle. If two rectangles overlap, the rectangle with the higher zIndex will appear on top
927
928
        $rectangle_output = '';
929
930
        foreach ($params as $key => $value) {
931
            if (isset($rectangle[$key])) {
932
                $rectangle[$key] = $value;
933
            }
934
        }
935
936
        if ($rectangle['positionSW'] != "" && $rectangle['positionNE'] != "") {
937
            $lat_long_to_push = '';
938
            if ($this->is_lat_long($rectangle['positionSW'])) {
939
                $lat_long_to_push = $rectangle['positionSW'];
940
                $rectangle_output .= '
941
				var positionSW = new google.maps.LatLng('.$rectangle['positionSW'].')
942
				';
943
            } else {
944
                $lat_long = $this->get_lat_long_from_address($rectangle['positionSW']);
945
                $rectangle_output .= '
946
				var positionSW = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
947
                $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
948
            }
949
            $rectangle_output .= '
950
				lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
951
			';
952
953
            $lat_long_to_push = '';
954
            if ($this->is_lat_long($rectangle['positionNE'])) {
955
                $lat_long_to_push = $rectangle['positionNE'];
956
                $rectangle_output .= '
957
				var positionNE = new google.maps.LatLng('.$rectangle['positionNE'].')
958
				';
959
            } else {
960
                $lat_long = $this->get_lat_long_from_address($rectangle['positionNE']);
961
                $rectangle_output .= '
962
				var positionNE = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
963
                $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
964
            }
965
            $rectangle_output .= '
966
				lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
967
			';
968
969
            $rectangle_output .= '
970
				var rectangleOptions = {
971
					strokeColor: "'.$rectangle['strokeColor'].'",
972
					strokeOpacity: '.$rectangle['strokeOpacity'].',
973
					strokeWeight: '.$rectangle['strokeWeight'].',
974
					fillColor: "'.$rectangle['fillColor'].'",
975
					fillOpacity: '.$rectangle['fillOpacity'].',
976
					map: '.$this->map_name.',
977
					bounds: new google.maps.LatLngBounds(positionSW, positionNE)';
978
            if (!$rectangle['clickable']) {
979
                $rectangle_output .= ',
980
					clickable: false';
981
            }
982
            if ($rectangle['zIndex'] != "" && is_numeric($rectangle['zIndex'])) {
983
                $rectangle_output .= ',
984
					zIndex: '.$rectangle['zIndex'];
985
            }
986
            $rectangle_output .= '
987
				};';
988
989
            $rectangle_output .= '
990
				var rectangle_'.count($this->rectangles).' = new google.maps.Rectangle(rectangleOptions);
991
			';
992
993
            if ($rectangle['onclick'] != "") {
994
                $rectangle_output .= '
995
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "click", function() {
996
					'.$rectangle['onclick'].'
997
				});
998
				';
999
            }
1000
            if ($rectangle['ondblclick'] != "") {
1001
                $rectangle_output .= '
1002
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "dblclick", function() {
1003
					'.$rectangle['ondblclick'].'
1004
				});
1005
				';
1006
            }
1007
            if ($rectangle['onmousedown'] != "") {
1008
                $rectangle_output .= '
1009
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "mousedown", function() {
1010
					'.$rectangle['onmousedown'].'
1011
				});
1012
				';
1013
            }
1014
            if ($rectangle['onmousemove'] != "") {
1015
                $rectangle_output .= '
1016
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "mousemove", function() {
1017
					'.$rectangle['onmousemove'].'
1018
				});
1019
				';
1020
            }
1021
            if ($rectangle['onmouseout'] != "") {
1022
                $rectangle_output .= '
1023
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "mouseout", function() {
1024
					'.$rectangle['onmouseout'].'
1025
				});
1026
				';
1027
            }
1028
            if ($rectangle['onmouseover'] != "") {
1029
                $rectangle_output .= '
1030
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "mouseover", function() {
1031
					'.$rectangle['onmouseover'].'
1032
				});
1033
				';
1034
            }
1035
            if ($rectangle['onmouseup'] != "") {
1036
                $rectangle_output .= '
1037
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "mouseup", function() {
1038
					'.$rectangle['onmouseup'].'
1039
				});
1040
				';
1041
            }
1042
            if ($rectangle['onrightclick'] != "") {
1043
                $rectangle_output .= '
1044
				google.maps.event.addListener(rectangle_'.count($this->rectangles).', "rightclick", function() {
1045
					'.$rectangle['onrightclick'].'
1046
				});
1047
				';
1048
            }
1049
1050
            array_push($this->rectangles, $rectangle_output);
1051
        }
1052
    }
1053
1054
    public function add_ground_overlay($params = array())
1055
    {
1056
        $overlay = array();
1057
1058
        $overlay['image'] = '';                                    // JavaScript performed when a ground overlay is clicked
1059
        $overlay['positionSW'] = '';                            // The South-West position (latitude/longitude coordinate OR addresse) at which the ground overlay will appear
1060
        $overlay['positionNE'] = '';                            // The North-East position (latitude/longitude coordinate OR addresse) at which the ground overlay will appear
1061
        $overlay['clickable'] = true;                            // Defines if the ground overlay is clickable
1062
        $overlay['onclick'] = '';                                // JavaScript performed when a ground overlay is clicked
1063
1064
        $overlay_output = '';
1065
1066
        foreach ($params as $key => $value) {
1067
            if (isset($overlay[$key])) {
1068
                $overlay[$key] = $value;
1069
            }
1070
        }
1071
1072
        if ($overlay['image'] != "" && $overlay['positionSW'] != "" && $overlay['positionNE'] != "") {
1073
            $lat_long_to_push = '';
1074
            if ($this->is_lat_long($overlay['positionSW'])) {
1075
                $lat_long_to_push = $overlay['positionSW'];
1076
                $overlay_output .= '
1077
				var positionSW = new google.maps.LatLng('.$overlay['positionSW'].')
1078
				';
1079
            } else {
1080
                $lat_long = $this->get_lat_long_from_address($overlay['positionSW']);
1081
                $overlay_output .= '
1082
				var positionSW = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
1083
                $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
1084
            }
1085
            $overlay_output .= '
1086
				lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
1087
			';
1088
1089
            $lat_long_to_push = '';
1090
            if ($this->is_lat_long($overlay['positionNE'])) {
1091
                $lat_long_to_push = $overlay['positionNE'];
1092
                $overlay_output .= '
1093
				var positionNE = new google.maps.LatLng('.$overlay['positionNE'].')
1094
				';
1095
            } else {
1096
                $lat_long = $this->get_lat_long_from_address($overlay['positionNE']);
1097
                $overlay_output .= '
1098
				var positionNE = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')';
1099
                $lat_long_to_push = $lat_long[0].', '.$lat_long[1];
1100
            }
1101
            $overlay_output .= '
1102
				lat_longs_'.$this->map_name.'.push(new google.maps.LatLng('.$lat_long_to_push.'));
1103
			';
1104
1105
            $overlay_output .= '
1106
				var overlay_'.count($this->overlays).' = new google.maps.GroundOverlay("'.$overlay['image'].'", new google.maps.LatLngBounds(positionSW, positionNE), { map: '.$this->map_name;
1107
            if (!$overlay['clickable']) {
1108
                $overlay_output .= ', clickable: false';
1109
            }
1110
            $overlay_output .= '});
1111
			';
1112
1113
            if ($overlay['onclick'] != "") {
1114
                $overlay_output .= '
1115
				google.maps.event.addListener(overlay_'.count($this->overlays).', "click", function() {
1116
					'.$overlay['onclick'].'
1117
				});
1118
				';
1119
            }
1120
1121
            array_push($this->overlays, $overlay_output);
1122
        }
1123
    }
1124
1125
    public function create_map()
1126
    {
1127
        $this->output_js = '';
1128
        $this->output_js_contents = '';
1129
        $this->output_html = "";
1130
1131
        if ($this->maps_loaded == 0) {
1132
            if ($this->apiKey != "") {
1133
                $apiLocation = 'https://maps.googleapis.com/maps/api/js?key='.$this->apiKey.'&';
1134
            } else {
1135
                $apiLocation = 'https://maps.google.com/maps/api/js?';
1136
            }
1137
            if ($this->region != "" && strlen($this->region) == 2) {
1138
                $apiLocation .= '&region='.strtoupper($this->region);
1139
            }
1140
            if ($this->language != "") {
1141
                $apiLocation .= '&language='.$this->language;
1142
            }
1143
            $libraries = array();
1144
            if ($this->adsense != "") {
1145
                array_push($libraries, 'adsense');
1146
            }
1147
            if ($this->places != "") {
1148
                array_push($libraries, 'places');
1149
            }
1150
            if ($this->panoramio) {
1151
                array_push($libraries, 'panoramio');
1152
            }
1153
            if ($this->drawing) {
1154
                array_push($libraries, 'drawing');
1155
            }
1156
            if (count($libraries)) {
1157
                $apiLocation .= '&libraries='.implode(",", $libraries);
1158
            }
1159
1160
            if (!$this->loadAsynchronously) {
1161
                $this->output_js .= '
1162
				<script type="text/javascript" src="'.$apiLocation.'"></script>';
1163
            }
1164
1165
            if ($this->cluster) {
1166
                $this->output_js .= '
1167
1168
			<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-marker-clusterer/1.0.0/markerclusterer_compiled.js"></script >
1169
					';
1170
            }
1171
        }
1172
        if ($this->jsfile == "") {
1173
            $this->output_js .= '
1174
			<script type="text/javascript">
1175
			//<![CDATA[
1176
			';
1177
        }
1178
1179
        $this->output_js_contents .= '
1180
			var '.$this->map_name.'; // Global declaration of the map
1181
			var lat_longs_'.$this->map_name.' = new Array();
1182
			var markers_'.$this->map_name.' = new Array();
1183
            var iw_'.$this->map_name.';
1184
			';
1185
        if ($this->cluster) {
1186
            $this->output_js_contents .= 'var markerCluster;
1187
			';
1188
        }
1189
        if ($this->directions) {
1190
            $rendererOptions = '';
1191
            if ($this->directionsDraggable) {
1192
                $this->output_js_contents .= '
1193
			var rendererOptions = { draggable: true };
1194
			';
1195
                $rendererOptions = 'rendererOptions';
1196
            }
1197
            $this->output_js_contents .= 'var directionsDisplay = new google.maps.DirectionsRenderer('.$rendererOptions.');
1198
			var directionsService = new google.maps.DirectionsService();
1199
			';
1200
        }
1201
        if ($this->places) {
1202
            $this->output_js_contents .= 'var placesService;
1203
			';
1204
            if ($this->placesAutocompleteInputID != "") {
1205
                $this->output_js_contents .= 'var placesAutocomplete;
1206
			';
1207
            }
1208
        }
1209
        if ($this->adsense) {
1210
            $this->output_js_contents .= 'var adUnit;
1211
			';
1212
        }
1213
        if ($this->drawing) {
1214
            $this->output_js_contents .= 'var drawingManager;
1215
			';
1216
        }
1217
1218
        $this->output_js_contents .= '
1219
			iw_'.$this->map_name.' = new google.maps.InfoWindow(';
1220
        if ($this->infowindowMaxWidth != 0) {
1221
            $this->output_js_contents .= '{
1222
				maxWidth: '.$this->infowindowMaxWidth.'
1223
			}';
1224
        }
1225
        $this->output_js_contents .= ');
1226
1227
				 ';
1228
1229
        $this->output_js_contents .= 'function initialize_'.$this->map_name.'() {
1230
1231
				';
1232
1233
        $styleOutput = '';
1234
        if (count($this->styles)) {
1235
            $styles = 0;
1236
            foreach ($this->styles as $style) {
1237
                $this->output_js_contents .= 'var styles_'.$styles.' = '.json_encode($style['definition']).';
1238
				';
1239
1240
                if ($this->stylesAsMapTypes) {
1241
                    $this->output_js_contents .= 'var styles_'.$styles.' = new google.maps.StyledMapType(styles_'.$styles.', {name:"'.$style['name'].'"});
1242
				';
1243
                } else {
1244
                    $styleOutput .= $this->map_name.'.setOptions({styles: styles_'.$styles.'});
1245
				';
1246
                    break;
1247
                }
1248
1249
                ++$styles;
1250
            }
1251
        }
1252
1253
        if ($this->center != "auto") {
1254
            if ($this->is_lat_long($this->center)) { // if centering the map on a lat/long
1255
                $this->output_js_contents .= 'var myLatlng = new google.maps.LatLng('.$this->center.');';
1256
            } else {  // if centering the map on an address
1257
                $lat_long = $this->get_lat_long_from_address($this->center);
1258
                $this->output_js_contents .= 'var myLatlng = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].');';
1259
            }
1260
        }
1261
1262
        $this->output_js_contents .= '
1263
				var myOptions = {
1264
			  		';
1265
        if ($this->zoom == "auto") {
1266
            $this->output_js_contents .= 'zoom: 13,';
1267
        } else {
1268
            $this->output_js_contents .= 'zoom: '.$this->zoom.',';
1269
        }
1270
        if ($this->center != "auto") {
1271
            $this->output_js_contents .= '
1272
					center: myLatlng,';
1273
        }
1274
        if($this->gestureHandling != 'auto'){
1275
            $this->output_js_contents .= '
1276
                    gestureHandling: \''.$this->gestureHandling .'\',';
1277
        }
1278
        if (strtolower($this->map_type) == "street") {
1279
            $map_type = "ROADMAP";
1280
        } else {
1281
            $map_type = $this->map_type;
1282
        }
1283
        $this->output_js_contents .= '
1284
			  		mapTypeId: google.maps.MapTypeId.'.$map_type;
1285
        if ($this->backgroundColor) {
1286
            $this->output_js_contents .= ',
1287
					backgroundColor: \''.$this->backgroundColor.'\'';
1288
        }
1289
        if ($this->disableDefaultUI) {
1290
            $this->output_js_contents .= ',
1291
					disableDefaultUI: true';
1292
        }
1293
        if ($this->disableMapTypeControl) {
1294
            $this->output_js_contents .= ',
1295
					mapTypeControl: false';
1296
        }
1297
        if ($this->disableNavigationControl) {
1298
            $this->output_js_contents .= ',
1299
					navigationControl: false';
1300
        }
1301
        if ($this->disableScaleControl) {
1302
            $this->output_js_contents .= ',
1303
					scaleControl: false';
1304
        }
1305
        if ($this->disableStreetViewControl) {
1306
            $this->output_js_contents .= ',
1307
					streetViewControl: false';
1308
        }
1309
        if ($this->disableDoubleClickZoom) {
1310
            $this->output_js_contents .= ',
1311
					disableDoubleClickZoom: true';
1312
        }
1313
        if (!$this->draggable) {
1314
            $this->output_js_contents .= ',
1315
					draggable: false';
1316
        }
1317
        if ($this->draggableCursor != "") {
1318
            $this->output_js_contents .= ',
1319
					draggableCursor: "'.$this->draggableCursor.'"';
1320
        }
1321
        if ($this->draggingCursor != "") {
1322
            $this->output_js_contents .= ',
1323
					draggingCursor: "'.$this->draggingCursor.'"';
1324
        }
1325
        if (!$this->keyboardShortcuts) {
1326
            $this->output_js_contents .= ',
1327
					keyboardShortcuts: false';
1328
        }
1329
        $mapTypeControlOptions = array();
1330
        $map_types = array();
1331
        if ($this->mapTypeControlPosition != "") {
1332
            array_push($mapTypeControlOptions, 'position: google.maps.ControlPosition.'.strtoupper($this->mapTypeControlPosition));
1333
        }
1334
        if ($this->mapTypeControlStyle != "" && (strtoupper($this->mapTypeControlStyle) == "DROPDOWN_MENU" || strtoupper($this->mapTypeControlStyle) == "HORIZONTAL_BAR")) {
1335
            array_push($mapTypeControlOptions, 'style: google.maps.MapTypeControlStyle.'.strtoupper($this->mapTypeControlStyle));
1336
        }
1337
        if (count($this->map_types_available)) {
1338
            foreach ($this->map_types_available as $map_type) {
1339
                array_push($map_types, 'google.maps.MapTypeId.'.strtoupper($map_type));
1340
            }
1341
        }
1342
        if (count($this->styles) && $this->stylesAsMapTypes) {
1343
            $styles = 0;
1344
            foreach ($this->styles as $style) {
1345
                array_push($map_types, '"style'.$styles.'"');
1346
                $styleOutput .= '
1347
					  '.$this->map_name.'.mapTypes.set("style'.$styles.'", styles_'.$styles.');
1348
				';
1349
                if ($this->stylesAsMapTypesDefault == $style['name']) {
1350
                    $styleOutput .= '
1351
					  '.$this->map_name.'.setMapTypeId("style'.$styles.'");
1352
				';
1353
                }
1354
                $styles++;
1355
            }
1356
        }
1357
        if (count($map_types)) {
1358
            array_push($mapTypeControlOptions, 'mapTypeIds: ['.implode(", ", $map_types).']');
1359
        }
1360
        if (count($mapTypeControlOptions)) {
1361
            $this->output_js_contents .= ',
1362
					mapTypeControlOptions: {'.implode(",", $mapTypeControlOptions).'}';
1363
        }
1364
        if ($this->minzoom != "") {
1365
            $this->output_js_contents .= ',
1366
					minZoom: '.$this->minzoom;
1367
        }
1368
        if ($this->maxzoom != "") {
1369
            $this->output_js_contents .= ',
1370
					maxZoom: '.$this->maxzoom;
1371
        }
1372
        if ($this->noClear) {
1373
            $this->output_js_contents .= ',
1374
					noClear: true';
1375
        }
1376
        if ($this->navigationControlPosition != "") {
1377
            $this->output_js_contents .= ',
1378
					navigationControlOptions: {position: google.maps.ControlPosition.'.strtoupper($this->navigationControlPosition).'}';
1379
        }
1380
        if ($this->scaleControlPosition != "") {
1381
            $this->output_js_contents .= ',
1382
			         scaleControl: true,
1383
					scaleControlOptions: {position: google.maps.ControlPosition.'.strtoupper($this->scaleControlPosition).'}';
1384
        }
1385
        if (!$this->scrollwheel) {
1386
            $this->output_js_contents .= ',
1387
					scrollwheel: false';
1388
        }
1389
        if ($this->streetViewControlPosition != "") {
1390
            $this->output_js_contents .= ',
1391
					streetViewControlOptions: {position: google.maps.ControlPosition.'.strtoupper($this->streetViewControlPosition).'}';
1392
        }
1393
        if ($this->tilt == 45) {
1394
            $this->output_js_contents .= ',
1395
					tilt: '.$this->tilt;
1396
        }
1397
        $zoomControlOptions = array();
1398
        if ($this->zoomControlPosition != "") {
1399
            array_push($zoomControlOptions, 'position: google.maps.ControlPosition.'.strtoupper($this->zoomControlPosition));
1400
        }
1401
        if ($this->zoomControlStyle != "" && (strtoupper($this->zoomControlStyle) == "SMALL" || strtoupper($this->zoomControlStyle) == "LARGE")) {
1402
            array_push($zoomControlOptions, 'style: google.maps.ZoomControlStyle.'.strtoupper($this->zoomControlStyle));
1403
        }
1404
        if (count($zoomControlOptions)) {
1405
            $this->output_js_contents .= ',
1406
					zoomControlOptions: {'.implode(",", $zoomControlOptions).'}';
1407
        }
1408
1409
1410
        $this->output_js_contents .= '};';
1411
1412
        $this->output_js_contents .=$this->map_name.' = new google.maps.Map(document.getElementById("'.$this->map_div_id.'"), myOptions);';
1413
1414
        if ($this->tiledOverlayLayer) {
1415
            $this->output_js_contents .= "{$this->map_name}.overlayMapTypes.insertAt(0, maptiler);";
1416
        }
1417
1418
1419
        if ($styleOutput != "") {
1420
            $this->output_js_contents .= $styleOutput.'
1421
				';
1422
        }
1423
1424
        if ($this->trafficOverlay) {
1425
            $this->output_js_contents .= 'var trafficLayer = new google.maps.TrafficLayer();
1426
				trafficLayer.setMap('.$this->map_name.');
1427
				';
1428
        }
1429
        if ($this->bicyclingOverlay) {
1430
            $this->output_js_contents .= 'var bikeLayer = new google.maps.BicyclingLayer();
1431
				bikeLayer.setMap('.$this->map_name.');
1432
				';
1433
        }
1434
1435
        if ((is_array($this->kmlLayerURL) && count($this->kmlLayerURL)) || (!is_array($this->kmlLayerURL) && $this->kmlLayerURL != "")) {
1436
            if (!is_array($this->kmlLayerURL)) {
1437
                // Need to convert single KML layer to array
1438
                $this->kmlLayerURL = array($this->kmlLayerURL);
1439
            }
1440
            if (count($this->kmlLayerURL)) {
1441
                $i = 0;
1442
                foreach ($this->kmlLayerURL as $kmlLayerURL) {
1443
                    $this->output_js_contents .= '
1444
						var kmlLayerOptions = {
1445
							map: '.$this->map_name;
1446
                    if ($this->kmlLayerPreserveViewport) {
1447
                        $this->output_js_contents .= ',
1448
							preserveViewport: true';
1449
                    }
1450
                    $this->output_js_contents .= '
1451
						}
1452
						var kmlLayer_'.$i.' = new google.maps.KmlLayer("'.$kmlLayerURL.'", kmlLayerOptions);
1453
						';
1454
                    ++$i;
1455
                }
1456
            }
1457
        }
1458
1459
        if ($this->panoramio) {
1460
            $this->output_js_contents .= 'var panoramioLayer = new google.maps.panoramio.PanoramioLayer();
1461
				';
1462
            if ($this->panoramioTag != "") {
1463
                $this->output_js_contents .= 'panoramioLayer.setTag("'.$this->panoramioTag.'");
1464
				';
1465
            }
1466
            if ($this->panoramioUser != "") {
1467
                $this->output_js_contents .= 'panoramioLayer.setUserId("'.$this->panoramioUser.'");
1468
				';
1469
            }
1470
            $this->output_js_contents .= '
1471
				panoramioLayer.setMap('.$this->map_name.');
1472
				';
1473
        }
1474
1475
        if (strtolower($this->map_type) == "street") { // if defaulting the map to Street View
1476
            $this->output_js_contents .= '
1477
			  	var streetViewOptions = {
1478
			    	position: myLatlng';
1479
            if (!$this->streetViewAddressControl) {
1480
                $this->output_js_contents .= ',
1481
					addressControl: false';
1482
            }
1483
            if ($this->streetViewAddressPosition != "") {
1484
                $this->output_js_contents .= ',
1485
					addressControlOptions: { position: google.maps.ControlPosition.'.$this->streetViewAddressPosition.' }';
1486
            }
1487
            if ($this->streetViewCloseButton) {
1488
                $this->output_js_contents .= ',
1489
					enableCloseButton: true';
1490
            }
1491
            if (!$this->streetViewLinksControl) {
1492
                $this->output_js_contents .= ',
1493
					linksControl: false';
1494
            }
1495
            if (!$this->streetViewPanControl) {
1496
                $this->output_js_contents .= ',
1497
					panControl: false';
1498
            }
1499
            if ($this->streetViewPanPosition != "") {
1500
                $this->output_js_contents .= ',
1501
					panControlOptions: { position: google.maps.ControlPosition.'.$this->streetViewPanPosition.' }';
1502
            }
1503
            if ($this->streetViewPovHeading != 0 || $this->streetViewPovPitch != 0 || $this->streetViewPovZoom != 0) {
1504
                $this->output_js_contents .= ',
1505
					pov: {
1506
						heading: '.$this->streetViewPovHeading.',
1507
						pitch: '.$this->streetViewPovPitch.',
1508
						zoom: '.$this->streetViewPovZoom.'
1509
					}';
1510
            }
1511
            if (!$this->streetViewZoomControl) {
1512
                $this->output_js_contents .= ',
1513
					zoomControl: false';
1514
            }
1515
            if ($this->streetViewZoomPosition != "" || $this->streetViewZoomStyle != "") {
1516
                $this->output_js_contents .= ',
1517
					zoomControlOptions: {';
1518
                if ($this->streetViewZoomPosition != "") {
1519
                    $this->output_js_contents .= '
1520
						position: google.maps.ControlPosition.'.$this->streetViewZoomPosition.',';
1521
                }
1522
                if ($this->streetViewZoomStyle != "") {
1523
                    $this->output_js_contents .= '
1524
						style: google.maps.ZoomControlStyle.'.$this->streetViewZoomStyle.',';
1525
                }
1526
                $this->output_js_contents = trim($this->output_js_contents, ",");
1527
                $this->output_js_contents .= '}';
1528
            }
1529
            $this->output_js_contents .= '
1530
				};
1531
				var streetView = new google.maps.StreetViewPanorama(document.getElementById("'.$this->map_div_id.'"), streetViewOptions);
1532
				streetView.setVisible(true);
1533
	      		';
1534
        }
1535
1536
        if ($this->center == "auto") { // if wanting to center on the users location
1537
            $this->output_js_contents .= '
1538
				// Try W3C Geolocation (Preferred)
1539
				if(navigator.geolocation) {
1540
					navigator.geolocation.getCurrentPosition(function(position) {
1541
						'.$this->map_name.'.setCenter(new google.maps.LatLng(position.coords.latitude,position.coords.longitude));
1542
					}, function() { alert("Unable to get your current position. Please try again. Geolocation service failed."); });
1543
				// Browser doesn\'t support Geolocation
1544
				}else{
1545
					alert(\'Your browser does not support geolocation.\');
1546
				}
1547
			';
1548
        }
1549
1550
        if ($this->directions) {
1551
            $this->output_js_contents .= 'directionsDisplay.setMap('.$this->map_name.');
1552
			';
1553
            if ($this->directionsDivID != "") {
1554
                $this->output_js_contents .= 'directionsDisplay.setPanel(document.getElementById("'.$this->directionsDivID.'"));
1555
			';
1556
            }
1557
            if ($this->directionsDraggable && $this->directionsChanged != "") {
1558
                $this->output_js_contents .= 'google.maps.event.addListener(directionsDisplay, "directions_changed", function() {
1559
					'.$this->directionsChanged.'
1560
				});
1561
			';
1562
            }
1563
        }
1564
1565
        if ($this->drawing) {
1566
            if ($this->drawingControlPosition == '') {
1567
                $this->drawingControlPosition = 'TOP_CENTER';
1568
            }
1569
1570
            $this->output_js_contents .= 'drawingManager = new google.maps.drawing.DrawingManager({
1571
				drawingMode: google.maps.drawing.OverlayType.'.strtoupper($this->drawingDefaultMode).',
1572
  				drawingControl: '.(!$this->drawingControl ? 'false' : 'true').',
1573
  				drawingControlOptions: {
1574
  					position: google.maps.ControlPosition.'.strtoupper($this->drawingControlPosition);
1575
            $shapeOptions = '';
1576
            if (count($this->drawingModes)) {
1577
                $this->output_js_contents .= ',
1578
					drawingModes: [';
1579
                $i = 0;
1580
                foreach ($this->drawingModes as $drawingMode) {
1581
                    if ($i > 0) {
1582
                        $this->output_js_contents .= ',';
1583
                    }
1584
                    $this->output_js_contents .= 'google.maps.drawing.OverlayType.'.strtoupper($drawingMode);
1585
                    if (strtoupper($drawingMode) != "MARKER") {
1586
                        $shapeOptions .= ',
1587
						'.strtolower($drawingMode).'Options: {
1588
							editable: true
1589
						}';
1590
                    }
1591
                    $i++;
1592
                }
1593
                $this->output_js_contents .= ']';
1594
            }
1595
            $this->output_js_contents .= '
1596
				}'.$shapeOptions.'
1597
			});
1598
			drawingManager.setMap('.$this->map_name.');
1599
			';
1600
1601
            $this->output_js_contents .= '
1602
			google.maps.event.addListener(drawingManager, "overlaycomplete", function(event) {
1603
				var newShape = event.overlay;
1604
				newShape.type = event.type;
1605
				';
1606
            if (count($this->drawingOnComplete)) {
1607
                foreach ($this->drawingOnComplete as $shape => $js) {
1608
                    $this->output_js_contents .= 'if (event.type==google.maps.drawing.OverlayType.'.strtoupper($shape).') {
1609
						'.$js.'
1610
					}
1611
					';
1612
                }
1613
            }
1614
1615
            if (count($this->drawingOnEdit)) {
1616
                if (isset($this->drawingOnEdit['polygon'])) {
1617
                    $this->output_js_contents .= '
1618
				if (newShape.type==google.maps.drawing.OverlayType.POLYGON) {
1619
					var newShapePaths = newShape.getPaths();
1620
					for (var i=0; i<newShapePaths.length; i++) {
1621
						google.maps.event.addListener(newShapePaths.getAt(i), "set_at", function(event) {
1622
							'.$this->drawingOnEdit['polygon'].'
1623
						});
1624
						google.maps.event.addListener(newShapePaths.getAt(i), "insert_at", function(event) {
1625
							'.$this->drawingOnEdit['polygon'].'
1626
						});
1627
						google.maps.event.addListener(newShapePaths.getAt(i), "remove_at", function(event) {
1628
							'.$this->drawingOnEdit['polygon'].'
1629
						});
1630
					}
1631
				}';
1632
                }
1633
                if (isset($this->drawingOnEdit['polyline'])) {
1634
                    $this->output_js_contents .= '
1635
				if (newShape.type==google.maps.drawing.OverlayType.POLYLINE) {
1636
					var newShapePaths = newShape.getPaths();
1637
					for (var i=0; i<newShapePaths.length; i++) {
1638
						google.maps.event.addListener(newShapePaths.getAt(i), "set_at", function(event) {
1639
							'.$this->drawingOnEdit['polyline'].'
1640
						});
1641
						google.maps.event.addListener(newShapePaths.getAt(i), "insert_at", function(event) {
1642
							'.$this->drawingOnEdit['polyline'].'
1643
						});
1644
						google.maps.event.addListener(newShapePaths.getAt(i), "remove_at", function(event) {
1645
							'.$this->drawingOnEdit['polyline'].'
1646
						});
1647
					}
1648
				}';
1649
                }
1650
                if (isset($this->drawingOnEdit['rectangle'])) {
1651
                    $this->output_js_contents .= '
1652
				if (newShape.type==google.maps.drawing.OverlayType.RECTANGLE) {
1653
					google.maps.event.addListener(newShape, "bounds_changed", function(event) {
1654
						'.$this->drawingOnEdit['rectangle'].'
1655
					});
1656
				}';
1657
                }
1658
                if (isset($this->drawingOnEdit['circle'])) {
1659
                    $this->output_js_contents .= '
1660
				if (newShape.type==google.maps.drawing.OverlayType.CIRCLE) {
1661
					google.maps.event.addListener(newShape, "radius_changed", function(event) {
1662
						'.$this->drawingOnEdit['circle'].'
1663
					});
1664
					google.maps.event.addListener(newShape, "center_changed", function(event) {
1665
						'.$this->drawingOnEdit['circle'].'
1666
					});
1667
				}';
1668
                }
1669
            }
1670
1671
            $this->output_js_contents .= '
1672
			});';
1673
        }
1674
1675
        if ($this->places) {
1676
            $placesLocationSet = false;
1677
1678
            if ($this->placesLocationSW != "" && $this->placesLocationNE != "") { // if search based on bounds
1679
1680
                $placesLocationSet = true;
1681
1682
                if ($this->is_lat_long($this->placesLocationSW)) {
1683
                    $this->output_js_contents .= 'var placesLocationSW = new google.maps.LatLng('.$this->placesLocationSW.');
1684
			';
1685
                } else {  // if centering the map on an address
1686
                    $lat_long = $this->get_lat_long_from_address($this->placesLocationSW);
1687
                    $this->output_js_contents .= 'var placesLocationSW = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].');
1688
			';
1689
                }
1690
1691
                if ($this->is_lat_long($this->placesLocationNE)) {
1692
                    $this->output_js_contents .= 'var placesLocationNE = new google.maps.LatLng('.$this->placesLocationNE.');
1693
			';
1694
                } else {  // if centering the map on an address
1695
                    $lat_long = $this->get_lat_long_from_address($this->placesLocationNE);
1696
                    $this->output_js_contents .= 'var placesLocationNE = new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].');
1697
			';
1698
                }
1699
            }
1700
1701
            if (($placesLocationSet || $this->placesLocation != "") || count($this->placesTypes) || $this->placesName != "") {
1702
                $this->output_js_contents .= 'var placesRequest = {
1703
					';
1704
                if ($placesLocationSet) {
1705
                    $this->output_js_contents .= 'bounds: new google.maps.LatLngBounds(placesLocationSW, placesLocationNE)
1706
						';
1707
                } else {
1708
                    if ($this->placesLocation != "") { // if search based on a center point
1709
                        if ($this->is_lat_long($this->placesLocation)) { // if centering the map on a lat/long
1710
                            $this->output_js_contents .= 'location: new google.maps.LatLng('.$this->placesLocation.')
1711
						';
1712
                        } else {  // if centering the map on an address
1713
                            $lat_long = $this->get_lat_long_from_address($this->placesLocation);
1714
                            $this->output_js_contents .= 'location: new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')
1715
						';
1716
                        }
1717
                        $this->output_js_contents .= ',radius: '.$this->placesRadius.'
1718
						';
1719
                    }
1720
                }
1721
1722
                if (count($this->placesTypes)) {
1723
                    $this->output_js_contents .= ',types: [\''.implode("','", $this->placesTypes).'\']
1724
						';
1725
                }
1726
                if ($this->placesName != "") {
1727
                    $this->output_js_contents .= ',name : \''.$this->placesName.'\'
1728
						';
1729
                }
1730
                $this->output_js_contents .= '};
1731
1732
				placesService = new google.maps.places.PlacesService('.$this->map_name.');
1733
				placesService.search(placesRequest, placesCallback);
1734
				';
1735
            }
1736
1737
            if ($this->placesAutocompleteInputID != "") {
1738
                $this->output_js_contents .= 'var autocompleteOptions = {
1739
					';
1740
                $autocompleteOptions = '';
1741
                if ($this->placesAutocompleteBoundSW != "" && $this->placesAutocompleteBoundNE != "") {
1742
                    if ($this->is_lat_long($this->placesAutocompleteBoundSW)) {
1743
                        $autocompleteOptionsSW = 'new google.maps.LatLng('.$this->placesAutocompleteBoundSW.')
1744
					';
1745
                    } else {  // if centering the map on an address
1746
                        $lat_long = $this->get_lat_long_from_address($this->placesAutocompleteBoundSW);
1747
                        $autocompleteOptionsSW = 'new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')
1748
					';
1749
                    }
1750
1751
                    if ($this->is_lat_long($this->placesAutocompleteBoundNE)) {
1752
                        $autocompleteOptionsNE = 'new google.maps.LatLng('.$this->placesAutocompleteBoundNE.')
1753
					';
1754
                    } else {  // if centering the map on an address
1755
                        $lat_long = $this->get_lat_long_from_address($this->placesAutocompleteBoundNE);
1756
                        $autocompleteOptionsNE = 'new google.maps.LatLng('.$lat_long[0].', '.$lat_long[1].')
1757
					';
1758
                    }
1759
                    $autocompleteOptions .= 'bounds: new google.maps.LatLngBounds('.$autocompleteOptionsSW.', '.$autocompleteOptionsNE.')';
1760
                }
1761
                if (count($this->placesAutocompleteTypes)) {
1762
                    if ($autocompleteOptions != "") {
0 ignored issues
show
introduced by
The condition $autocompleteOptions != '' can never be true.
Loading history...
1763
                        $autocompleteOptions .= ',
1764
						 ';
1765
                    }
1766
                    $autocompleteOptions .= 'types: [\''.implode("','", $this->placesAutocompleteTypes).'\']';
1767
                }
1768
                $this->output_js_contents .= $autocompleteOptions;
1769
                $this->output_js_contents .= '}';
1770
1771
                $this->output_js_contents .= '
1772
				var autocompleteInput = document.getElementById(\''.$this->placesAutocompleteInputID.'\');
1773
1774
				placesAutocomplete = new google.maps.places.Autocomplete(autocompleteInput, autocompleteOptions);
1775
				';
1776
1777
                if ($this->placesAutocompleteBoundsMap) {
1778
                    $this->output_js_contents .= 'placesAutocomplete.bindTo(\'bounds\', map);
1779
					';
1780
                }
1781
1782
                if ($this->placesAutocompleteOnChange != "") {
1783
                    $this->output_js_contents .= 'google.maps.event.addListener(placesAutocomplete, \'place_changed\', function() {
1784
						'.$this->placesAutocompleteOnChange.'
1785
					});
1786
					';
1787
                }
1788
            }
1789
        }
1790
1791
        if ($this->onboundschanged != "") {
1792
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "bounds_changed", function(event) {
1793
    			'.$this->onboundschanged.'
1794
  			});
1795
			';
1796
        }
1797
        if ($this->oncenterchanged != "") {
1798
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "center_changed", function(event) {
1799
    			'.$this->oncenterchanged.'
1800
  			});
1801
			';
1802
        }
1803
        if ($this->onclick != "") {
1804
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "click", function(event) {
1805
    			'.$this->onclick.'
1806
  			});
1807
			';
1808
        }
1809
        if ($this->ondblclick != "") {
1810
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "dblclick", function(event) {
1811
    			'.$this->ondblclick.'
1812
  			});
1813
			';
1814
        }
1815
        if ($this->ondrag != "") {
1816
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "drag", function(event) {
1817
    			'.$this->ondrag.'
1818
  			});
1819
			';
1820
        }
1821
        if ($this->ondragend != "") {
1822
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "dragend", function(event) {
1823
    			'.$this->ondragend.'
1824
  			});
1825
			';
1826
        }
1827
        if ($this->ondragstart != "") {
1828
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "dragstart", function(event) {
1829
    			'.$this->ondragstart.'
1830
  			});
1831
			';
1832
        }
1833
        if ($this->onidle != "") {
1834
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "idle", function(event) {
1835
    			'.$this->onidle.'
1836
  			});
1837
			';
1838
        }
1839
        if ($this->onmousemove != "") {
1840
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "mousemove", function(event) {
1841
    			'.$this->onmousemove.'
1842
  			});
1843
			';
1844
        }
1845
        if ($this->onmouseout != "") {
1846
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "mouseout", function(event) {
1847
    			'.$this->onmouseout.'
1848
  			});
1849
			';
1850
        }
1851
        if ($this->onmouseover != "") {
1852
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "mouseover", function(event) {
1853
    			'.$this->onmouseover.'
1854
  			});
1855
			';
1856
        }
1857
        if ($this->onresize != "") {
1858
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "resize", function(event) {
1859
    			'.$this->onresize.'
1860
  			});
1861
			';
1862
        }
1863
        if ($this->onrightclick != "") {
1864
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "rightclick", function(event) {
1865
    			'.$this->onrightclick.'
1866
  			});
1867
			';
1868
        }
1869
        if ($this->ontilesloaded != "") {
1870
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "tilesloaded", function(event) {
1871
    			'.$this->ontilesloaded.'
1872
  			});
1873
			';
1874
        }
1875
        if ($this->onzoomchanged != "") {
1876
            $this->output_js_contents .= 'google.maps.event.addListener('.$this->map_name.', "zoom_changed", function(event) {
1877
    			'.$this->onzoomchanged.'
1878
  			});
1879
			';
1880
        }
1881
1882
        // add markers
1883
        if (count($this->markers)) {
1884
            foreach ($this->markers as $marker) {
1885
                $this->output_js_contents .= $marker;
1886
            }
1887
        }
1888
        //
1889
1890
        if ($this->cluster) {
1891
            $this->output_js_contents .= '
1892
			var clusterOptions = {
1893
				gridSize: '.$this->clusterGridSize;
1894
            if ($this->clusterMaxZoom != "") {
1895
                $this->output_js_contents .= ',
1896
				maxZoom: '.$this->clusterMaxZoom;
1897
            }
1898
            if (!$this->clusterZoomOnClick) {
1899
                $this->output_js_contents .= ',
1900
				zoomOnClick: false';
1901
            }
1902
            if ($this->clusterAverageCenter) {
1903
                $this->output_js_contents .= ',
1904
				averageCenter: true';
1905
            }
1906
            if (count($this->clusterStyles) > 0) {
1907
1908
                $this->output_js_contents .= ',
1909
				styles: [ ';
1910
                $styleOutput = [];
1911
                foreach($this->clusterStyles as $clusterStyle){
1912
                    $attributes =[];
1913
                    foreach($clusterStyle as $key => $style){
1914
                        $attributes[] = $key.':"'.$style.'"';
1915
                    }
1916
                    $styleOutput[] = '{'.implode(',',$attributes).'}';
1917
                }
1918
                $this->output_js_contents .= implode(',',$styleOutput);
1919
                $this->output_js_contents .= ']';
1920
            }
1921
1922
            $this->output_js_contents .= ',
1923
				minimumClusterSize: '.$this->clusterMinimumClusterSize.'
1924
			};
1925
			markerCluster = new MarkerClusterer('.$this->map_name.', markers_'.$this->map_name.', clusterOptions);
1926
			';
1927
        }
1928
1929
        // add polylines
1930
        if (count($this->polylines)) {
1931
            foreach ($this->polylines as $polyline) {
1932
                $this->output_js_contents .= $polyline;
1933
            }
1934
        }
1935
        //
1936
1937
        // add polygons
1938
        if (count($this->polygons)) {
1939
            foreach ($this->polygons as $polygon) {
1940
                $this->output_js_contents .= $polygon;
1941
            }
1942
        }
1943
        //
1944
1945
        // add circles
1946
        if (count($this->circles)) {
1947
            foreach ($this->circles as $circle) {
1948
                $this->output_js_contents .= $circle;
1949
            }
1950
        }
1951
        //
1952
1953
        // add rectangles
1954
        if (count($this->rectangles)) {
1955
            foreach ($this->rectangles as $rectangle) {
1956
                $this->output_js_contents .= $rectangle;
1957
            }
1958
        }
1959
        //
1960
1961
        // add ground overlays
1962
        if (count($this->overlays)) {
1963
            foreach ($this->overlays as $overlay) {
1964
                $this->output_js_contents .= $overlay;
1965
            }
1966
        }
1967
        //
1968
1969
        if ($this->zoom == "auto") {
1970
            $this->output_js_contents .= '
1971
			fitMapToBounds_'.$this->map_name.'();
1972
			';
1973
        }
1974
1975
        if ($this->adsense) {
1976
            $this->output_js_contents .= '
1977
			var adUnitDiv = document.createElement("div");
1978
1979
		    // Note: replace the publisher ID noted here with your own
1980
		    // publisher ID.
1981
		    var adUnitOptions = {
1982
		    	format: google.maps.adsense.AdFormat.'.$this->adsenseFormat.',
1983
		    	position: google.maps.ControlPosition.'.$this->adsensePosition.',
1984
		    	publisherId: "'.$this->adsensePublisherID.'",
1985
		    	';
1986
            if ($this->adsenseChannelNumber != "") {
1987
                $this->output_js_contents .= 'channelNumber: "'.$this->adsenseChannelNumber.'",
1988
		    	';
1989
            }
1990
            $this->output_js_contents .= 'map: '.$this->map_name.',
1991
		    	visible: true
1992
		    };
1993
		    adUnit = new google.maps.adsense.AdUnit(adUnitDiv, adUnitOptions);
1994
		    ';
1995
        }
1996
1997
        if ($this->directions && $this->directionsStart != "" && $this->directionsEnd != "") {
1998
            if ($this->directionsStart == "auto" && $this->directionsEnd == "auto") {
1999
                // Both start and finish are at the users current location
2000
                $this->output_js_contents .= '
2001
				// Try W3C Geolocation (Preferred)
2002
				if(navigator.geolocation) {
2003
					navigator.geolocation.getCurrentPosition(function(position) {
2004
						start = position.coords.latitude+","+position.coords.longitude;
2005
						calcRoute(start, start);
2006
					}, function() { alert("Unable to get your current position. Please try again. Geolocation service failed."); });
2007
				// Browser doesn\'t support Geolocation
2008
				}else{
2009
					alert(\'Your browser does not support geolocation.\');
2010
				}
2011
				';
2012
            } elseif ($this->directionsStart == "auto") {
2013
                // The start point should be at the users current location
2014
                $this->output_js_contents .= '
2015
				// Try W3C Geolocation (Preferred)
2016
				if(navigator.geolocation) {
2017
					navigator.geolocation.getCurrentPosition(function(position) {
2018
						start = position.coords.latitude+","+position.coords.longitude;
2019
						calcRoute(start, \''.$this->directionsEnd.'\');
2020
					}, function() { alert("Unable to get your current position. Please try again. Geolocation service failed."); });
2021
				// Browser doesn\'t support Geolocation
2022
				}else{
2023
					alert(\'Your browser does not support geolocation.\');
2024
				}
2025
				';
2026
            } elseif ($this->directionsEnd == "auto") {
2027
                // The end point should be at the users current location
2028
                $this->output_js_contents .= '
2029
				// Try W3C Geolocation (Preferred)
2030
				if(navigator.geolocation) {
2031
					navigator.geolocation.getCurrentPosition(function(position) {
2032
						end = position.coords.latitude+","+position.coords.longitude;
2033
						calcRoute(\''.$this->directionsStart.'\', end);
2034
					}, function() { alert("Unable to get your current position. Please try again. Geolocation service failed."); });
2035
				// Browser doesn\'t support Geolocation
2036
				}else{
2037
					alert(\'Your browser does not support geolocation.\');
2038
				}
2039
				';
2040
            } else {
2041
                // The start and end point are at pre-defined locations
2042
                $this->output_js_contents .= '
2043
				calcRoute(\''.$this->directionsStart.'\', \''.$this->directionsEnd.'\');
2044
				';
2045
            }
2046
        }
2047
2048
        if ($this->onload != "") {
2049
            $this->output_js_contents .= '
2050
				'.$this->onload;
2051
        }
2052
2053
        $this->output_js_contents .= '
2054
2055
			}
2056
2057
		';
2058
2059
        // add markers
2060
        $this->output_js_contents .= '
2061
		function createMarker_'.$this->map_name.'(markerOptions) {
2062
			var marker = new google.maps.Marker(markerOptions);
2063
			markers_'.$this->map_name.'.push(marker);
2064
			lat_longs_'.$this->map_name.'.push(marker.getPosition());
2065
			return marker;
2066
		}
2067
		';
2068
        //
2069
2070
        if ($this->directions) {
2071
            $this->output_js_contents .= 'function calcRoute(start, end) {
2072
2073
			var request = {
2074
			    	origin:start,
2075
			    	destination:end,
2076
			    	travelMode: google.maps.TravelMode.'.$this->directionsMode.'
2077
			    	';
2078
2079
            if (count($this->directionsWaypointArray)) {
2080
                $directionsWaypointStr = '';
2081
                foreach ($this->directionsWaypointArray as $waypoint) {
2082
                    if ($directionsWaypointStr != '') {
2083
                        $directionsWaypointStr .= ',';
2084
                    }
2085
                    $directionsWaypointStr .= '{ location: "'.$waypoint.'", stopover: true}';
2086
                }
2087
                $this->output_js_contents .= ', waypoints: ['.$directionsWaypointStr.']';
2088
2089
                if ($this->directionsWaypointsOptimize) {
2090
                    $this->output_js_contents .= ', optimizeWaypoints: true';
2091
                }
2092
            }
2093
            if ($this->region != "" && strlen($this->region) == 2) {
2094
                $this->output_js_contents .= ',region: '.strtoupper($this->region).'
2095
					';
2096
            }
2097
            if (trim($this->directionsUnits) != "" && (strtolower(trim($this->directionsUnits)) == "metric" || strtolower(trim($this->directionsUnits)) == "imperial")) {
2098
                $this->output_js_contents .= ',unitSystem: google.maps.UnitSystem.'.strtoupper(trim($this->directionsUnits)).'
2099
					';
2100
            }
2101
            if ($this->directionsAvoidTolls) {
2102
                $this->output_js_contents .= ',avoidTolls: true
2103
					';
2104
            }
2105
            if ($this->directionsAvoidHighways) {
2106
                $this->output_js_contents .= ',avoidHighways: true
2107
					';
2108
            }
2109
2110
            $this->output_js_contents .= '
2111
			};
2112
			  	directionsService.route(request, function(response, status) {
2113
			    	if (status == google.maps.DirectionsStatus.OK) {
2114
			      		directionsDisplay.setDirections(response);
2115
			    	}else{
2116
			    		switch (status) {
2117
			    			case "NOT_FOUND": { alert("Either the start location or destination were not recognised"); break }
2118
			    			case "ZERO_RESULTS": { alert("No route could be found between the start location and destination"); break }
2119
			    			case "MAX_WAYPOINTS_EXCEEDED": { alert("Maximum waypoints exceeded. Maximum of 8 allowed"); break }
2120
			    			case "INVALID_REQUEST": { alert("Invalid request made for obtaining directions"); break }
2121
			    			case "OVER_QUERY_LIMIT": { alert("This webpage has sent too many requests recently. Please try again later"); break }
2122
			    			case "REQUEST_DENIED": { alert("This webpage is not allowed to request directions"); break }
2123
			    			case "UNKNOWN_ERROR": { alert("Unknown error with the server. Please try again later"); break }
2124
			    		}
2125
			    	}
2126
			  	});
2127
			}
2128
			';
2129
        }
2130
2131
        if ($this->places) {
2132
            $this->output_js_contents .= 'function placesCallback(results, status) {
2133
				if (status == google.maps.places.PlacesServiceStatus.OK) {
2134
					for (var i = 0; i < results.length; i++) {
2135
2136
						var place = results[i];
2137
2138
						var placeLoc = place.geometry.location;
2139
						var placePosition = new google.maps.LatLng(placeLoc.lat(), placeLoc.lng());
2140
						var markerOptions = {
2141
				 			map: '.$this->map_name.',
2142
				        	position: placePosition
2143
				      	};
2144
				      	var marker = createMarker_'.$this->map_name.'(markerOptions);
2145
				      	marker.set("content", place.name);
2146
				      	google.maps.event.addListener(marker, "click", function() {
2147
				        	iw_'.$this->map_name.'.setContent(this.get("content"));
2148
				        	iw_'.$this->map_name.'.open('.$this->map_name.', this);
2149
				      	});
2150
2151
				      	lat_longs_'.$this->map_name.'.push(placePosition);
2152
2153
					}
2154
					';
2155
            if ($this->zoom == "auto") {
2156
                $this->output_js_contents .= 'fitMapToBounds_'.$this->map_name.'();';
2157
            }
2158
            $this->output_js_contents .= '
2159
				}
2160
			}
2161
			';
2162
        }
2163
2164
        if ($this->zoom == "auto") {
2165
            $this->output_js_contents .= '
2166
			function fitMapToBounds_'.$this->map_name.'() {
2167
				var bounds = new google.maps.LatLngBounds();
2168
				if (lat_longs_'.$this->map_name.'.length>0) {
2169
					for (var i=0; i<lat_longs_'.$this->map_name.'.length; i++) {
2170
						bounds.extend(lat_longs_'.$this->map_name.'[i]);
2171
					}
2172
					'.$this->map_name.'.fitBounds(bounds);
2173
				}
2174
			}
2175
			';
2176
        }
2177
2178
        if ($this->loadAsynchronously) {
2179
            $this->output_js_contents .= '
2180
			function loadScript_'.$this->map_name.'() {
2181
				var script = document.createElement("script");
2182
  				script.type = "text/javascript";
2183
  				script.src = "'.$apiLocation.'&callback=initialize_'.$this->map_name.'";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiLocation does not seem to be defined for all execution paths leading up to this point.
Loading history...
2184
  				document.body.appendChild(script);
2185
			}
2186
			window.onload = loadScript_'.$this->map_name.';
2187
			';
2188
        } else {
2189
            $this->output_js_contents .= $this->tiledOverlayLayer;
2190
            $this->output_js_contents .= '
2191
			google.maps.event.addDomListener(window, "load", initialize_'.$this->map_name.');
2192
			';
2193
        }
2194
2195
        // Minify the Javascript if the $minifyJS config value is true. Requires Jsmin.php and PHP 5+
2196
        if ($this->minifyJS) {
2197
            $CI = \App::make('jsmin');
2198
            $this->output_js_contents = $CI->jsmin->min($this->output_js_contents);
2199
        }
2200
2201
        if ($this->jsfile == "") {
2202
            $this->output_js .= $this->output_js_contents;
2203
        } else { // if needs writing to external js file
2204
            if (!$handle = fopen($this->jsfile, "w")) {
2205
                $this->output_js .= $this->output_js_contents;
2206
            } else {
2207
                if (!fwrite($handle, $this->output_js_contents)) {
2208
                    $this->output_js .= $this->output_js_contents;
2209
                } else {
2210
                    $this->output_js .= '
2211
					<script src="'.$this->jsfile.'" type="text/javascript"></script>';
2212
                }
2213
            }
2214
        }
2215
2216
        if ($this->jsfile == "") {
2217
            $this->output_js .= '
2218
			//]]>
2219
			</script>';
2220
        }
2221
2222
        // set height and width
2223
        if (is_numeric($this->map_width)) { // if no width type set
2224
            $this->map_width = $this->map_width.'px';
2225
        }
2226
        if (is_numeric($this->map_height)) { // if no height type set
2227
            $this->map_height = $this->map_height.'px';
2228
        }
2229
        //
2230
2231
        $this->output_html .= '<div id="'.$this->map_div_id.'" style="width:'.$this->map_width.'; height:'.$this->map_height.';"'.(($this->class != "") ? ' class="'.$this->class.'"' : '').'></div>';
2232
2233
        ++$this->maps_loaded;
2234
2235
        return array('js' => $this->output_js, 'html' => $this->output_html, 'markers' => $this->markersInfo);
2236
    }
2237
2238
    public function is_lat_long($input)
2239
    {
2240
        $input = str_replace(", ", ",", trim($input));
2241
        $input = explode(",", $input);
2242
        if (count($input) == 2) {
2243
            if (is_numeric($input[0]) && is_numeric($input[1])) { // is a lat long
2244
                return true;
2245
            } else { // not a lat long - incorrect values
2246
                return false;
2247
            }
2248
        } else { // not a lat long - too many parts
2249
            return false;
2250
        }
2251
    }
2252
2253
    public function get_lat_long_from_address($address, $attempts = 0)
2254
    {
2255
        $lat = 0;
2256
        $lng = 0;
2257
2258
        $error = '';
2259
2260
        if ($this->geocodeCaching) { // if caching of geocode requests is activated
2261
2262
            $CI = & get_instance();
0 ignored issues
show
Bug introduced by
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2262
            $CI = & /** @scrutinizer ignore-call */ get_instance();
Loading history...
2263
            $CI->load->database();
2264
            $CI->db->select("latitude,longitude");
2265
            $CI->db->from("geocoding");
2266
            $CI->db->where("address", trim(strtolower($address)));
2267
            $query = $CI->db->get();
2268
2269
            if ($query->num_rows() > 0) {
2270
                $row = $query->row();
2271
2272
                return array($row->latitude, $row->longitude);
2273
            }
2274
        }
2275
	//utf8_encode($address) will return only english adress mean it's take only english address.
2276
    	// Remove utf8_encode from urlencode then it'll support all languages(eg. en, ur, chinese, russian, japanese, greek etc.)
2277
        // $data_location = "https://maps.google.com/maps/api/geocode/json?address=".urlencode(utf8_encode($address)); //Old One just for english
2278
	    $data_location = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address); // New One for every language.
2279
        if ($this->region != "" && strlen($this->region) == 2) {
2280
            $data_location .= "&region=".$this->region;
2281
        }
2282
        $data = file_get_contents($data_location);
2283
2284
        $data = json_decode($data);
2285
2286
        if ($data->status == "OK") {
2287
            $lat = $data->results[0]->geometry->location->lat;
2288
            $lng = $data->results[0]->geometry->location->lng;
2289
2290
            if ($this->geocodeCaching) { // if we to need to cache this result
2291
                if ($address != "" && $lat != 0 && $lng != 0) {
2292
                    $data = array(
2293
                        "address" => trim(strtolower($address)),
2294
                        "latitude" => $lat,
2295
                        "longitude" => $lng,
2296
                    );
2297
                    $CI->db->insert("geocoding", $data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $CI does not seem to be defined for all execution paths leading up to this point.
Loading history...
2298
                }
2299
            }
2300
        } else {
2301
            if ($data->status == "OVER_QUERY_LIMIT") {
2302
                $error = $data->status;
2303
                if ($attempts < 2) {
2304
                    sleep(1);
2305
                    ++$attempts;
2306
                    list($lat, $lng, $error) = $this->get_lat_long_from_address($address, $attempts);
2307
                }
2308
            }
2309
        }
2310
2311
        return array($lat, $lng, $error);
2312
    }
2313
}
2314