-
Notifications
You must be signed in to change notification settings - Fork 9
/
geocoding_advanced.php
92 lines (78 loc) · 3.23 KB
/
geocoding_advanced.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
83
84
85
86
87
88
89
90
91
92
<?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\GeocodeException'
);
// Autoloader stuff
require( '_system/autoload.php' );
// If a location is set, geocode it
if ( isset( $_GET['location'] ) && strlen( $_GET['location'] ) ) {
$geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $_GET['location'] );
if ( $geocode_result instanceof \PHPGoogleMaps\Service\GeocodeResult ) {
// If more than one result was found we are going to give the user an option to pick one
if ( count( $geocode_result->response->results ) > 1 ) {
$location = $_GET['location'];
$location_options = $geocode_result->response->results;
}
else {
$position = $geocode_result;
}
}
else {
$location = $geocode_result->location;
$error = $geocode_result->error;
}
}
if ( isset( $_GET['geocoded_location'] ) ) {
list( $location, $lat, $lng ) = explode( '|', $_GET['geocoded_location'] );
$position = new \PHPGoogleMaps\Core\LatLng( $lat, $lng, $location );
}
if ( isset( $position ) ) {
$map = new \PHPGoogleMaps\Map;
$marker = \PHPGoogleMaps\Overlay\Marker::createFromPosition( $position, array( 'content' => $position->location ) );
$map->addObject( $marker );
$map->disableAutoEncompass();
$map->setZoom( 13 );
$map->setCenter( $position );
}
?>
<!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>Advanced Geocoding</h1>
<?php require( '_system/nav.php' ) ?>
<p>Geocoder::geocode() returns the full geocode result from Google. This example uses the full result to ask the user to choose their desired address when multiple addresses fit the location they enter. <a href="?location=main+st">Main ST</a>, for example, will return multiple matching addresses.</p>
<form action="" method="get">
<?php if( isset( $location_options ) ): ?>
<p>There are <?php echo count( $location_options ) ?> locations that match <strong><?php echo $location ?></strong></p>
<label for="locations">Select a location</label>
<select name="geocoded_location">
<?php foreach( $location_options as $location_option ): ?>
<option value="<?php echo $location_option->formatted_address ?>|<?php echo $location_option->geometry->location->lat ?>|<?php echo $location_option->geometry->location->lng ?>"><?php echo $location_option->formatted_address ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Use this address">
<br><br>
<?php endif; ?>
<label for="location">Enter a location</label>
<input type="text" name="location">
<input type="submit" value=" Geocode ">
</form>
<?php if( isset( $position ) ): ?><p><?php echo $position->location ?></p><?php endif; ?>
<?php if( isset( $error ) ): ?><p>Unable to geocode "<?php echo $location ?>" (<?php echo $error ?>)</p><?php endif; ?>
<?php if( isset( $map ) ) $map->printMap() ?>
</body>
</html>