-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathautocomplete.php
More file actions
61 lines (47 loc) · 1.84 KB
/
autocomplete.php
File metadata and controls
61 lines (47 loc) · 1.84 KB
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
<?php
/*
This file will be used for listing autocomplete results.
Google autocomplete api (json) will be used to fetch data.
*/
Class Autocomplete{
private $api_key = '';
function __construct() {
$this->api_key = '<API KEY>';
}
public function suggest_location(){
header("Cache-Control: private, max-age=86400");
header("Expires: ".gmdate('r', time()+86400));
$query = $_GET["cityname"].' '.$_GET["q"];
$city_center_latlng = $_GET["city_center_latlng"];
$apikey = $this->api_key;
$url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?key='.$apikey.'&types=geocode&sensor=true&language=en&location='.$city_center_latlng.'&radius=12000&input='.urlencode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1 = curl_exec($ch);
curl_close($ch);
$details = json_decode($data1, true);
header("Content-Type: application/json");
$json = "{\"results\": [";
foreach($details['predictions'] as $key=>$row) {
$arr[] = "{\"id\": \"".$row['reference']."\", \"value\": \"".$row['description']."\"}";
}
$json .= implode(", ", $arr);
echo $json . "]}";
}
public function get_geocode(){
$apikey = $this->api_key;
$reference = $_GET["reference"];
$query = 'https://maps.googleapis.com/maps/api/place/details/json?reference='.urlencode($reference).'&sensor=true&key='.$apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
$details = json_decode($data, true);
$map['lat'] = $details['result']['geometry']['location']['lat'];
$map['long'] = $details['result']['geometry']['location']['lng'];
return json_encode($map);
}
}
?>