This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
EGMapDirection.php
177 lines (159 loc) · 6.01 KB
/
EGMapDirection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* EGMapDirection class
*
* @author Antonio Ramirez Cobos
* @link www.ramirezcobos.com
*
*
* @copyright
*
* Copyright (c) 2010 Antonio Ramirez Cobos
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class EGMapDirection extends EGMapBase {
const TRAVEL_MODE_DRIVING = 'google.maps.DirectionsTravelMode.DRIVING';
const TRAVEL_MODE_WALKING = 'google.maps.DirectionsTravelMode.WALKING';
const TRAVEL_MODE_BICYCLING = 'google.maps.DirectionsTravelMode.BICYCLING';
const UNIT_SYSTEM_IMPERIAL = 'google.maps.DirectionsUnitSystem.IMPERIAL';
const UNIT_SYSTEM_METRIC = 'google.maps.DirectionsUnitSystem.METRIC';
protected $renderer = null;
protected $options = array(
// Whether or not trip alternatives should be provided.
'avoidHighways' => null,
// If true, instructs the Directions service to avoids toll roads where possible. Optional.
'avoidTolls' => null,
// Location of destination. This can be specified as either a string to be geocoded or a LatLng. Required.
'destination' => null,
// If set to true, the DirectionService will attempt to re-order the supplied intermediate waypoints to
// minimize overall cost of the route. If waypoints are optimized, inspect DirectionsRoute.waypoint_order
// in the response to determine the new ordering.
'optimizeWaypoints' => null,
// Location of origin. This can be specified as either a string to be geocoded or a LatLng. Required.
'origin' => null,
// Whether or not route alternatives should be provided. Optional.
'provideRouteAlternatives' => null,
// Region code used as a bias for geocoding requests.
'region' => null,
// Travel mode [DRIVING, WALKING, BICYCLING]
'travelMode' => self::TRAVEL_MODE_WALKING,
// Preferred unit system to use when displaying distance.
// Defaults to the unit system used in the country of origin.
'unitSystem' => null,
// Array of intermediate waypoints. Directions will be calculated from the origin to the destination by way of each waypoint in this array.
'waypoints' => array(),
);
/**
* Construct GMapDirection object
*
* @param EGMapCoord $origin The coordinates of origin
* @param EGMapCoord $destination The coordinates of destination
* @param string $js_name The js var name
* @param array $options Array of option
* @author Vincent Guillon <[email protected]>
* @since 2009-10-30 17:20:47
* @since 2011-01-24 by Antonio Ramirez www.ramirezcobos.com
* New algorithms
*
*/
public function __construct(EGMapCoord $origin, EGMapCoord $destination, $js_name = 'gmap_direction', $options = array())
{
$this->origin = $origin;
$this->destination = $destination;
$this->setOptions(array_merge($this->options, $options));
if ($js_name !== 'gmap_direction')
$this->setJsName($js_name);
}
public function setRenderer(EGMapDirectionRenderer $renderer)
{
$this->renderer = $renderer;
}
public function setOrigin(EGMapCoord $origin)
{
$this->options['origin'] = $origin;
}
public function setDestination(EGMapCoord $destination)
{
$this->options['destination'] = $destination;
}
/**
* Options setter
*
* @param array $options
* @author Vincent Guillon <[email protected]>
* @since 2009-11-13 15:39:46
* @since 2011-01-24 by Antonio Ramirez
* Modified algorithm
*/
public function setOptions($options = null)
{
if (isset($options['origin']))
{
$this->setOrigin($options['origin']);
unset($options['origin']);
}
if (isset($options['destination']))
{
$this->setDestination($options['destination']);
unset($options['destination']);
}
$this->options = array_merge($this->options, $options);
}
/**
* Options getter
*
* @return array $this->options
* @author Vincent Guillon <[email protected]>
* @since 2009-11-13 15:38:46
*/
public function getOptions()
{
return $this->options;
}
/**
* Generate js code for direction
* Inspired by the work of Vincent Guillon <[email protected]>
*
* @param string $map_js_name The google map js var name
* @return $js_code The generated js to display direction
* @author Antonio Ramirez
* @since 2010-01-24
*
*/
public function toJs($map_js_name = 'map')
{
if (null === $this->renderer)
throw new CException(Yii::t('EGMap', 'No Renderer Service has been provided'));
$options = $this->getOptions();
$js_name = $this->getJsName();
// set map to renderer
$this->renderer->map = $map_js_name;
// Construct js code
$js_code = '';
$js_code .= $this->renderer->toJs();
$js_code .= 'var ' . $js_name . ' = new google.maps.DirectionsService();' . "\n";
// building Request
$js_code .= 'var ' . $js_name . 'Request = ' . EGMap::encode($this->options) . ';' . "\n";
$js_code .= $js_name . '.route(' . $js_name . 'Request, function(response, status)' . "\n";
$js_code .= '{' . "\n";
$js_code .= ' if (status == google.maps.DirectionsStatus.OK)' . "\n";
$js_code .= ' {' . "\n";
$js_code .= ' ' . $this->renderer->getJsName() . '.setDirections(response);' . "\n";
$js_code .= ' }' . "\n";
$js_code .= '});' . "\n";
return $js_code;
}
}