Skip to content

Commit 56a1a94

Browse files
committed
added better location handling
1 parent e456286 commit 56a1a94

File tree

3 files changed

+91
-45
lines changed

3 files changed

+91
-45
lines changed

dist/breinify-api.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15330,40 +15330,61 @@ dependencyScope.jQuery = $;;
1533015330
});
1533115331

1533215332
var _privates = {
15333+
foundGeoLocation: false,
15334+
geoLocation: null,
1533315335

1533415336
resolveGeoLocation: function (callback) {
15337+
var _self = this;
15338+
15339+
if (_self.foundGeoLocation) {
15340+
return _self.geoLocation;
15341+
}
1533515342

1533615343
var geo = navigator.geolocation;
1533715344
//noinspection JSUnresolvedVariable
1533815345
var permissions = navigator.permissions;
1533915346

1534015347
// make sure we have a geolocation implementation
1534115348
if (typeof geo !== 'object') {
15349+
_self.foundGeoLocation = true;
1534215350
callback(null);
1534315351
} else if (typeof permissions !== 'object') {
15352+
_self.foundGeoLocation = true;
1534415353
callback(null);
1534515354
} else {
1534615355

15347-
// check if the permission is already granted
15348-
permissions.query({name: 'geolocation'}).then(function (permission) {
15349-
if (permission.state === 'granted') {
15350-
geo.getCurrentPosition(
15351-
function (position) {
15352-
callback({
15353-
'accuracy': position.coords.accuracy,
15354-
'latitude': position.coords.latitude,
15355-
'longitude': position.coords.longitude,
15356-
'speed': position.coords.speed
15356+
// any error should be caught here
15357+
try {
15358+
15359+
// check if the permission is already granted
15360+
permissions.query({name: 'geolocation'}).then(function (permission) {
15361+
if (permission.state === 'granted') {
15362+
geo.getCurrentPosition(
15363+
function (position) {
15364+
_self.foundGeoLocation = true;
15365+
_self.geoLocation = {
15366+
'accuracy': position.coords.accuracy,
15367+
'latitude': position.coords.latitude,
15368+
'longitude': position.coords.longitude,
15369+
'speed': position.coords.speed
15370+
};
15371+
15372+
callback(_self.geoLocation);
15373+
}, function () {
15374+
_self.foundGeoLocation = true;
15375+
callback(null)
15376+
}, {
15377+
'timeout': 150
1535715378
});
15358-
}, function () {
15359-
callback(null)
15360-
}, {
15361-
'timeout': 150
15362-
});
15363-
} else {
15364-
callback(null);
15365-
}
15366-
});
15379+
} else {
15380+
_self.foundGeoLocation = true;
15381+
callback(null);
15382+
}
15383+
});
15384+
} catch (e) {
15385+
_self.foundGeoLocation = true;
15386+
callback(null);
15387+
}
1536715388
}
1536815389
}
1536915390
};
@@ -15421,7 +15442,9 @@ dependencyScope.jQuery = $;;
1542115442
}
1542215443

1542315444
// try to set the location if there isn't one yet
15424-
if (instance.read('location') === null && $.isFunction(onReady)) {
15445+
var location = instance.read('location');
15446+
var hasLocation = $.isPlainObject(location) && typeof location.latitude === 'number' && typeof location.longitude === 'number';
15447+
if (!hasLocation && $.isFunction(onReady)) {
1542515448
instance.addGeoLocation(onReady);
1542615449
} else if ($.isFunction(onReady)) {
1542715450
onReady(instance);

dist/breinify-api.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BreinifyUser.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,40 +95,61 @@
9595
});
9696

9797
var _privates = {
98+
foundGeoLocation: false,
99+
geoLocation: null,
98100

99101
resolveGeoLocation: function (callback) {
102+
var _self = this;
103+
104+
if (_self.foundGeoLocation) {
105+
return _self.geoLocation;
106+
}
100107

101108
var geo = navigator.geolocation;
102109
//noinspection JSUnresolvedVariable
103110
var permissions = navigator.permissions;
104111

105112
// make sure we have a geolocation implementation
106113
if (typeof geo !== 'object') {
114+
_self.foundGeoLocation = true;
107115
callback(null);
108116
} else if (typeof permissions !== 'object') {
117+
_self.foundGeoLocation = true;
109118
callback(null);
110119
} else {
111120

112-
// check if the permission is already granted
113-
permissions.query({name: 'geolocation'}).then(function (permission) {
114-
if (permission.state === 'granted') {
115-
geo.getCurrentPosition(
116-
function (position) {
117-
callback({
118-
'accuracy': position.coords.accuracy,
119-
'latitude': position.coords.latitude,
120-
'longitude': position.coords.longitude,
121-
'speed': position.coords.speed
121+
// any error should be caught here
122+
try {
123+
124+
// check if the permission is already granted
125+
permissions.query({name: 'geolocation'}).then(function (permission) {
126+
if (permission.state === 'granted') {
127+
geo.getCurrentPosition(
128+
function (position) {
129+
_self.foundGeoLocation = true;
130+
_self.geoLocation = {
131+
'accuracy': position.coords.accuracy,
132+
'latitude': position.coords.latitude,
133+
'longitude': position.coords.longitude,
134+
'speed': position.coords.speed
135+
};
136+
137+
callback(_self.geoLocation);
138+
}, function () {
139+
_self.foundGeoLocation = true;
140+
callback(null)
141+
}, {
142+
'timeout': 150
122143
});
123-
}, function () {
124-
callback(null)
125-
}, {
126-
'timeout': 150
127-
});
128-
} else {
129-
callback(null);
130-
}
131-
});
144+
} else {
145+
_self.foundGeoLocation = true;
146+
callback(null);
147+
}
148+
});
149+
} catch (e) {
150+
_self.foundGeoLocation = true;
151+
callback(null);
152+
}
132153
}
133154
}
134155
};
@@ -186,7 +207,9 @@
186207
}
187208

188209
// try to set the location if there isn't one yet
189-
if (instance.read('location') === null && $.isFunction(onReady)) {
210+
var location = instance.read('location');
211+
var hasLocation = $.isPlainObject(location) && typeof location.latitude === 'number' && typeof location.longitude === 'number';
212+
if (!hasLocation && $.isFunction(onReady)) {
190213
instance.addGeoLocation(onReady);
191214
} else if ($.isFunction(onReady)) {
192215
onReady(instance);

0 commit comments

Comments
 (0)