-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoLocation.js
42 lines (42 loc) · 1.74 KB
/
GeoLocation.js
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
var GeoLocation = function(){
this.latitude = 0;
this.longitude = 0;
this.street = "";
this.number = "";
this.location = "";
this.city = "";
this.state = "";
this.country = "";
this.postal_code = "";
var getLatLng = function(position) {
GeoLocation.latitude = position.coords.latitude;
GeoLocation.longitude = position.coords.longitude;
codeLatLng(GeoLocation.latitude, GeoLocation.longitude);
};
this.getLocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getLatLng);
} else {
alert('Navegador não suporta');
}
};
var codeLatLng = function (latitude,longitude){
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
GeoLocation.street = results[0].address_components[1].long_name;
GeoLocation.number = results[0].address_components[0].long_name;
GeoLocation.location = results[1].address_components[0].long_name;
GeoLocation.city = results[1].address_components[1].long_name;
GeoLocation.state = results[1].address_components[3].long_name;
GeoLocation.country = results[1].address_components[3].long_name;
GeoLocation.postal_code = results[3].address_components[0].long_name;
}
}else{
alert("Geocoder failed due to: " + status);
}
});
};
};