-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGooglegeocoder.php
More file actions
70 lines (61 loc) · 1.89 KB
/
Googlegeocoder.php
File metadata and controls
70 lines (61 loc) · 1.89 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
62
63
64
65
66
67
68
69
70
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Googlegeocoder {
/**
* GoogleGeocoder CI Class
*
*
* @package googlegeocoder
* @version 1.0.0
* @author Jay Chandra, Shubh Tech <shubhtech.in>
* @copyright Copyright (c) 2020, Jay Chandra
* @license http://www.opensource.org/licenses/mit-license.php
* @link http://github.com/jaychandra02/googlegeocoder/
*
*/
public function __construct() {
$this->baseURL = "https://maps.google.com/maps/api/geocode/json?key=YOUR_API_KEY";
}
public function reverseGeocode($location, $addrtype = 0) {
//$addrType : Response Format
//0=Full Address
//1=State, ZIP, Country
//2=Locality, City, State, Country
//3=City, State, Country
//4=State, Country
//5=Country
$url = $this->baseURL . "&latlng=" . $location;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
if ($contents) {
$resp = json_decode($contents);
if ($resp->status = 'OK') {
return $resp->results[$addrtype]->formatted_address;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
public function geocode($address) {
$url = $this->baseURL . "&address=" . urlencode($address);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
if ($contents) {
$resp = json_decode($contents);
if ($resp->status = 'OK') {
return $resp->results[0]->geometry->location;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
}
?>