-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.js
112 lines (103 loc) · 3.13 KB
/
geo.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
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
Coltan.Geo = {};
Coltan.Geo.getCurrentLocation = function(fn,purpose){
Coltan.debug('getting location');
Coltan.debug('Ti.Platform.model = '+Ti.Platform.model);
Coltan.debug('Ti.Platform.name = '+Ti.Platform.name);
Coltan.debug('Ti.Geolocation.locationServicesEnabled = '+Ti.Geolocation.locationServicesEnabled);
if(_.isIos()){
Ti.Geolocation.purpose = purpose || Coltan.Geo.purpose;
}
Ti.Geolocation.getCurrentPosition(function(e) {
var coords;
if (!e.success || e.error) {
if (true || Ti.Platform.model === 'google_sdk' || Ti.Geolocation.locationServicesEnabled === false) {
Coltan.info('in android simulator, using hard-coded location fixture');
coords = {
latitude: 40.7298602,
longitude: -73.9914152
};
} else {
Coltan.error(e.error);
fn(e.error);
}
} else {
coords = e.coords;
}
coords.latitude = Number(coords.latitude).toFixed(6);
coords.longitude = Number(coords.longitude).toFixed(6);
Ti.App.Properties.setDouble('latitude', coords.latitude);
Ti.App.Properties.setDouble('longitude', coords.longitude);
// Ti.App.Properties.setInt('location_updated_at',(+ new Date()));
Ti.App.fireEvent('location_updated', {
coords: coords
});
fn(null, coords);
});
};
Coltan.Geo.customReverseGeocode = function(coords,fn,context){
Coltan.Geo.GoogleMapClient.executeRequest({
uri:'/maps/api/geocode/json',
params:{
sensor:true,
latlng:coords.latitude+','+coords.longitude
}
},function(err,result){
if(err){
Coltan.error(err.message || err);
fn(err);
return;
};
if(result.status === 'OK'){
fn(null,result.results);
} else {
Coltan.error('Google geocode error:');
Coltan.error(JSON.stringify(result));
fn({message:'Unknown error'});
}
});
};
Coltan.Geo.getLatestCoords = function(){
return {
latitude:Ti.App.Properties.getDouble('latitude',40.7298602),
longitude:Ti.App.Properties.getDouble('longitude',-73.9914152),
// updatedAt:Ti.App.Properties.getInt('location_updated_at')
};
}
Coltan.Geo.makeGoogleBounds = function(coords){
if(coords == null){
coords = Coltan.Geo.getLatestCoords();
}
var lower = (coords.latitude - 0.1)+','+(coords.longitude - 0.1);
var upper = (coords.latitude + 0.1)+','+(coords.longitude + 0.1);
return lower+'|'+upper;
};
Coltan.Geo.customLocationSearch = function(str,fn){
Coltan.Geo.GoogleMapClient.executeRequest({
uri:'/maps/api/geocode/json',
params:{
sensor:true,
address:str,
bounds:Coltan.Geo.makeGoogleBounds()
}
},function(err,result){
if(err){
Coltan.error(err.message || err);
fn(err);
return;
};
if(result.status === 'OK'){
fn(null,result.results);
} else {
Coltan.error('Google geocode error:');
Coltan.error(JSON.stringify(result));
fn({message:'Unknown error'});
}
});
};
Coltan.Geo.createGoogleMapClient = function(){
Coltan.Geo.GoogleMapClient = new Coltan.HTTP.APIClient({
baseUrl:'http://maps.googleapis.com',
logLevel:1
});
};
Coltan.Geo.createGoogleMapClient();