1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class NearestPOIPage extends Page |
4
|
|
|
{ |
5
|
|
|
public static $has_one = array('PointsOfInterestLayer' => 'PointsOfInterestLayer'); |
6
|
|
|
|
7
|
1 |
|
public function getCMSFields() |
8
|
|
|
{ |
9
|
1 |
|
$fields = parent::getCMSFields(); |
10
|
1 |
|
$field = DropdownField::create('PointsOfInterestLayerID', 'PointsOfInterestLayer', |
11
|
1 |
|
PointsOfInterestLayer::get()->map('ID', 'Title')) |
12
|
1 |
|
->setEmptyString('-- Select one --'); |
13
|
1 |
|
$fields->addFieldToTab('Root.Layer', $field); |
14
|
|
|
|
15
|
1 |
|
return $fields; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
class NearestPOIPage_Controller extends Page_Controller |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
private static $allowed_actions = array('find'); |
|
|
|
|
22
|
|
|
|
23
|
|
|
/* |
24
|
|
|
For a given point of interest layer, find the 25 nearest places to provided location |
25
|
|
|
*/ |
26
|
1 |
|
public function find() |
27
|
|
|
{ |
28
|
1 |
|
$params = $this->request->getVars(); |
29
|
1 |
|
$latitude = $params['lat']; |
30
|
1 |
|
$longitude = $params['lng']; |
31
|
1 |
|
$sql = "SELECT DISTINCT poi.ID,Name,Lat,Lon, ( 6371 * acos( cos( radians({$latitude}) ) * cos( radians( Lat ) ) |
32
|
1 |
|
* cos( radians( Lon ) - radians({$longitude}) ) + sin( radians({$latitude}) ) * sin(radians(Lat)) ) ) AS distance |
33
|
|
|
FROM PointOfInterest poi |
34
|
|
|
INNER JOIN PointsOfInterestLayer_PointsOfInterest poilpoi |
35
|
|
|
ON poilpoi.PointOfInterestID = poi.ID |
36
|
1 |
|
WHERE PointsOfInterestLayerID={$this->PointsOfInterestLayerID} |
37
|
|
|
HAVING distance < 25 |
38
|
|
|
ORDER BY distance |
39
|
1 |
|
LIMIT 0 , 20;"; |
40
|
|
|
|
41
|
1 |
|
$records = DB::query($sql); |
42
|
1 |
|
$result = new ArrayList(); |
43
|
1 |
|
foreach ($records as $record) { |
44
|
1 |
|
$dob = new DataObject(); |
45
|
|
|
//number_format((float)$number, 2, '.', ''); |
46
|
1 |
|
$dob->Latitude = number_format((float) $record['Lat'], 3, '.', ''); |
47
|
1 |
|
$dob->Longitude = number_format((float) $record['Lon'], 3, '.', ''); |
48
|
1 |
|
$dob->Name = $record['Name']; |
49
|
1 |
|
$dob->Distance = number_format((float) $record['distance'], 2, '.', ''); |
50
|
1 |
|
$mapurl = "http://maps.google.com?q={$dob->Latitude},{$dob->Longitude}"; |
51
|
1 |
|
$dob->MapURL = $mapurl; |
52
|
1 |
|
$dirurl = "http://maps.google.com?saddr={$latitude},{$longitude}&daddr={$dob->Latitude},{$dob->Longitude}"; |
53
|
1 |
|
$dob->DirURL = $dirurl; |
54
|
1 |
|
$result->push($dob); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
$vars = new ArrayData(array('Nearest' => $result, 'Action' => $this->Action)); |
|
|
|
|
58
|
1 |
|
$this->Nearest = $result; |
59
|
|
|
|
60
|
1 |
|
return array(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.