Skip to content

Commit bdaa1d6

Browse files
committed
- (Feature) Added ability to plot directions responses directly to a map
1 parent e2edb26 commit bdaa1d6

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

models/GoogleMaps_DirectionsModel.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,72 @@
33

44
class GoogleMaps_DirectionsModel extends BaseModel
55
{
6+
public function plot($map)
7+
{
8+
foreach($this->routes as $route)
9+
{
10+
$locations = array();
11+
$markers = array();
12+
13+
foreach($route->legs as $index => $leg)
14+
{
15+
$icon = 'http://mt.google.com/vt/icon/text='.(chr(65 + $index)).'&psize=16&font=fonts/arialuni_t.ttf&color=ff330000&name=icons/spotlight/spotlight-waypoint-a.png&ax=44&ay=48&scale=2';
16+
17+
$locations[] = array(
18+
'address' => $leg->start_address,
19+
'lat' => $leg->start_location->lat,
20+
'lng' => $leg->start_location->lng,
21+
);
22+
23+
$markers[] = array(
24+
'content' => $leg->start_address,
25+
'lat' => $leg->start_location->lat,
26+
'lng' => $leg->start_location->lng,
27+
'icon' => $icon
28+
);
29+
30+
if($index == count($route->legs) - 1)
31+
{
32+
$icon = 'http://mt.google.com/vt/icon/text='.(chr(65 + $index + 1)).'&psize=16&font=fonts/arialuni_t.ttf&color=ff330000&name=icons/spotlight/spotlight-waypoint-b.png&ax=44&ay=48&scale=2';
33+
34+
$markers[] = array(
35+
'content' => $leg->end_address,
36+
'lat' => $leg->end_location->lat,
37+
'lng' => $leg->end_location->lng,
38+
'icon' => $icon
39+
);
40+
41+
$locations[] = array(
42+
'address' => $leg->end_address,
43+
'lat' => $leg->end_location->lat,
44+
'lng' => $leg->end_location->lng,
45+
);
46+
}
47+
}
48+
49+
craft()->templates->includeJs('
50+
new GoogleMaps.Route('.$map.', {
51+
fitBounds: true,
52+
locations: '.json_encode($locations).',
53+
markers: '.json_encode($markers).',
54+
directionsRendererOptions: {
55+
suppressMarkers: true,
56+
suppressInfoWindows: true,
57+
preserveViewport: true
58+
},
59+
directionsRequestOptions: '.json_encode($this->options).'
60+
});
61+
');
62+
63+
}
64+
}
65+
666
protected function defineAttributes()
767
{
868
return array(
9-
'routes' => array(AttributeType::Mixed, 'default' => array()),
10-
'status' => array(AttributeType::Mixed, 'default' => false),
69+
'routes' => array(AttributeType::Mixed, 'default' => array()),
70+
'options' => array(AttributeType::Mixed, 'default' => array()),
71+
'status'=> array(AttributeType::Mixed, 'default' => false),
1172
);
1273
}
1374
}

resources/js/plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,9 @@ var GoogleMaps = {
11051105
waypoints: this.getWaypoints()
11061106
};
11071107

1108-
request = _.extend(request, this.directionsRequestOptions);
1108+
request = _.extend(this.directionsRequestOptions, request);
1109+
1110+
console.log(request);
11091111

11101112
this.directionsService.route(request, function(response, status) {
11111113
t.lastResponse = response;

services/GoogleMaps_DirectionsService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class GoogleMaps_DirectionsService extends BaseApplicationComponent
77

88
public $expirationLength = 365; // In days
99

10+
protected $options = array();
11+
1012
public function route($options)
1113
{
1214
$url = $this->getUrl($options);
@@ -19,7 +21,7 @@ public function route($options)
1921

2022
$response = (string) $response->getResponse()->getBody();
2123

22-
return new GoogleMaps_DirectionsModel((array) json_decode($response));
24+
return new GoogleMaps_DirectionsModel(array_merge((array) json_decode($response), array('options' => $options)));
2325

2426
/*
2527
$encodedAddress = urlencode($address);

services/GoogleMaps_TemplatesService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class GoogleMaps_TemplatesService extends BaseApplicationComponent
55
{
66
public function scripts()
77
{
8-
craft()->templates->includeJsFile('//maps.google.com/maps/api/js?sensor=true');
8+
craft()->templates->includeJsFile('//maps.google.com/maps/api/js?sensor=true&libraries=geometry');
99
craft()->templates->includeJsResource('googlemaps/js/vendor/base.js');
1010
craft()->templates->includeJsResource('googlemaps/js/vendor/underscore.js');
1111
craft()->templates->includeJsResource('googlemaps/js/plugin.js');

0 commit comments

Comments
 (0)