-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocationSelect.html
111 lines (100 loc) · 3.21 KB
/
locationSelect.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
geocoder = new google.maps.Geocoder();
var address=[];
function updateCity() {
address=[];
if($('#city').val().length < 4) {
$('#finding-indicator').hide();
$('#address').html('');
} else {
getCity() ;
}
}
function getCity(){
return (geocoder.geocode( { 'address': $('#city').val()}, function(results, status) {
$('#finding-indicator').hide();
if (status == google.maps.GeocoderStatus.OK) {
var address_components = results[0].address_components ;
var cityFound=false ;
var regionFound=false ;
var countryFound = false ;
for (component in address_components) {
if(address_components[component].types[0] == 'locality') {
address.city = address_components[component].long_name;
cityFound=true ;
}
if(address_components[component].types[0] == 'administrative_area_level_1') {
address.region = address_components[component].long_name;
regionFound=true ;
}
if(address_components[component].types[0] == 'country') {
address.country = address_components[component].long_name;
countryFound = true ;
}
}
if (cityFound) {
// Construct friendly address
if (address.region == undefined) {
address.region = address.country ;
}
if (address.city == address.country) {
address.formatted = address.city ;
} else {
address.formatted = address.city+', '+address.region+', '+address.country ;
}
$('#google-address').val(address.formatted);
$('#locationJson').html(JSON.stringify(address_components));
}
if (address.formatted == undefined) {
if (address.region == undefined) {
$('#address').html('Where in '+address.country);
} else {
$('#address').html('Where in '+address.region);
}
} else {
$('#address').html(address.formatted);
}
} else {
$('#address').html('Not found !');
}
}));
}
$(document).ready(function(){
var myTimer = undefined;
var delayTime = 2000; // should be in miliseconds.
$('#city').keyup(function() {
if($('#city').val().length >= 4) {
$('#finding-indicator').show();
}
if (myTimer) {
clearTimeout(myTimer);
}
myTimer = setTimeout(function(){
updateCity();
}, delayTime);
});
$('#city').change(function() {
$('#finding-indicator').show();
updateCity();
});
});
</script>
</head>
<body>
<span style="font-size:16px">
Which city do you live ? <input type="text" id="city" name="city" value="" />
<span id="address">
</span>
<span id = "finding-indicator" style="font-size:10px;display:none;">Finding...</span>
<br/>
</span>
<div id="locationJson" style="font-size:10px;">
</div>
</body>
</html