-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostcode_finder.php
250 lines (174 loc) · 5.84 KB
/
postcode_finder.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!doctype html>
<html>
<head>
<title>Postcode Finder</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/
bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/
bootstrap-theme.min.css">
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDcznnMCsCMQI3NZb_velORbTJhIxLVJi8&libraries=places&signed_in=true">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<style>
html, body {
height: 100%;
}
#main_container {
background-image: url(gmap_background2.jpg);
height: 100%;
width: 100%;
background-size: cover;
background-position: center;
}
.center {
text-align: center;
}
.black {
color: black;
}
.white {
color: white;
}
#info_div {
padding-top: 50px;
}
button {
margin-top: 20px;
margin-bottom: 20px;
}
.form-group {
margin-top: 20px;
}
.alert {
margin-top: 27px;
display: none;
}
#map-canvas {
margin-top: 10px;
height: 45%;
width: 100%;
display: none;
}
</style>
<script>
// add autocomplete feature
var autocomplete;
function initialize() {
// Create the autocomplete object, restricting the search
// to geographical location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('input_address')),
{ types: ['geocode'] });
}
// [START region_geolocation]
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
<body onload="initialize()">
<div class="container" id="main_container">
<div class="row">
<div class="col-md-6 col-md-offset-3 center white" id="info_div">
<h1 class="center">Postcode Finder</h1>
</p class="lead center">please enter address below to find the postcode</p>
<from>
<div class="form-group">
<input type="text" class="form-control" name="address" id="input_address" placeholder="Please enter the address" onFocus="geolocate()"></input>
</div>
<button id="findmypostcode" class="btn btn-success btn-lg">Find The Postcode</button>
</from>
</div>
</div>
<div id="success" class="alert alert-success col-md-6 col-md-offset-3">yeah!!</div>
<div id="danger" class="alert alert-danger col-md-6 col-md-offset-3">Please enter a valid address</div>
<div class="center" id="map-canvas"></div>
</div>
<!-- including jquery-->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script>
$("#findmypostcode").click(function(event) {
//alert("working");
var checker = 0;
$(".alert").hide();
$("#map-canvas").slideUp();
event.preventDefault();
$.ajax({
type: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/xml?address=" + encodeURIComponent($('#input_address').val()) + "&key=AIzaSyDcznnMCsCMQI3NZb_velORbTJhIxLVJi8",
dataType: "xml",
success:processXML,
error: error
});
function error() {
alert("connection to google map database loss");
}
function processXML(xml) {
$(xml).find("address_component").each(function() {
if ($(this).find("type").text() == "postal_code") {
// find the postal code !!
$("#success").html("The postcode you looking for is " + ($(this).find("long_name").text())).fadeIn();
$("#map_icon").fadeIn();
//define latitude and logtitude
var latitude;
var longitude;
// find latitude and longitude in xml file
$(xml).find("location").each(function() {
latitude = $(this).find("lat").text();
longitude = $(this).find("lng").text();
})
// show the address location on google map
$("#map-canvas").slideDown(function() {
var myLatlng = new google.maps.LatLng(latitude,longitude);
var mapOptions = {
zoom: 15,
center: myLatlng
}
// display the map
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// define a marker on map
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: $('#input_address').val()
});
// define a infomation window
var infowindow = new google.maps.InfoWindow({
content: "this is the location of the place you searched."
});
// click the marker to show the infomation window
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
});
checker = 1;
}
});
if (checker == 0) {
$("#danger").fadeIn();
}
}
});
</script>
</body>
</html>