-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgeocoding_cached.php
84 lines (72 loc) · 2.35 KB
/
geocoding_cached.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
<?php
// This is just for my examples
require( '_system/config.php' );
$relevant_code = array(
'\PHPGoogleMaps\Service\Geocoder',
'\PHPGoogleMaps\Service\GeocodeError',
'\PHPGoogleMaps\Service\GeocodeResult',
'\PHPGoogleMaps\Service\GeocodeCachePDO',
'\PHPGoogleMaps\Service\GeocodeException',
'\PHPGoogleMaps\Service\CachingGeocoder',
'\PHPGoogleMaps\Service\CachedGeocodeResult'
);
// Autoloader stuff
require( '_system/autoload.php' );
// If location is set
if ( isset( $_GET['location'] ) && strlen( $_GET['location'] ) ) {
// Create a PDO Geocode Cache connection and pass it to the caching geocoder
try {
$geoPDO = new \PHPGoogleMaps\Service\GeocodeCachePDO( 'host', 'username', 'password', 'database' );
}
catch ( PDOException $e ) {
die( 'Unable to connect to database' );
}
$caching_geo = new \PHPGoogleMaps\Service\CachingGeocoder( $geoPDO );
// Geocode the location with the caching geocoded
$geocode_result = $caching_geo->geocode( $_GET['location'] );
if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
// Create a map
$map = new \PHPGoogleMaps\Map;
$marker = \PHPGoogleMaps\Overlay\Marker::createFromPosition( $geocode_result );
$map->addObject( $marker );
$map->disableAutoEncompass();
$map->setZoom( 13 );
$map->setCenter( $geocode_result );
}
else {
$location = $geocode_result->location;
$error = $geocode_result->error;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Geocoding - <?php echo PAGE_TITLE ?></title>
<link rel="stylesheet" type="text/css" href="_css/style.css">
<?php if( isset( $map ) ): ?>
<?php $map->printHeaderJS() ?>
<?php $map->printMapJS() ?>
<?php endif; ?>
</head>
<body>
<h1>Geocoding</h1>
<?php require( '_system/nav.php' ) ?>
<?php if( isset( $error ) ): ?>
<p>Unable to geocode "<?php echo $location ?>" (<?php echo $error ?>)</p>
<?php else: ?>
<?php if( isset( $map ) ): ?>
<h2><?php echo $geocode_result->location ?></h2>
<p>Was in cache: <?php echo $geocode_result->wasInCache() ? 'yes' : 'no' ?></p>
<p>Was put in cache: <?php echo $geocode_result->wasPutInCache() ? 'yes' : 'no' ?></p>
<?php endif; ?>
<?php endif; ?>
<form action="" method="get">
<label for="location">Enter a location</label>
<input type="text" name="location">
<input type="submit" value=" Geocode ">
</form>
<?php if( isset( $map ) ) $map->printMap() ?>
</body>
</html>