我的代码会检查当前用户的位置,然后使用cordova的地理位置插件加载附近的所有商店,包括距离的位置。
这是我的代码:
.factory('GSSearchLocationService',['$cordovaGeolocation', '$q',function($cordovaGeolocation, $q){
function _getLocation(options){
var callStart = options.callStart;
var deferred = options.deferred;
var attempt = options.attempt;
var othOptions = options.othOptions;
deferred.notify({attempt: attempt, message:'Searching attempt '+attempt, lastAccuracy : options.lastAccuracy});
var getLocOptions = {
enableHighAccuracy: othOptions.gps.enableHighAccuracy,
timeout: othOptions.gps.timeout * 100,
maximumAge: 0
};
var locWatch = $cordovaGeolocation.watchPosition(getLocOptions);
locWatch.then(
null,
function(err) {
locWatch.clearWatch();
deferred.reject({err:err});
},
function(position) {
var callEnd = new Date().getTime() / 1000;
locWatch.clearWatch();
if ( position.coords.accuracy && position.coords.accuracy <= othOptions.gps.accuracy ) {
// This is good accuracy then accept it
deferred.resolve({status:0, position:position});
} else if ( (callEnd - callStart) < othOptions.gps.timeout ) {
// Keep trying till the configured wait time. If exceeds then return back.
options.attempt++;
options.lastAccuracy = Math.round(position.coords.accuracy * 100) / 100;
options.minAccuracy = options.minAccuracy || options.lastAccuracy; // Default
options.minAccuracy = options.minAccuracy < options.lastAccuracy ? options.lastAccuracy : options.minAccuracy;
_getLocationWrapper(options);
} else {
othOptions.gps.timeout
deferred.reject( {error:{code:-999, message:"Could not get location.<br>Most min accuracy is "+options.minAccuracy+" mts.<br>Try to check location in open area or try adjusting to acceptable accuracy."}} );
}
}
);
}
function _getLocationWrapper(options) {
var locCB=function(){return _getLocation(options);};
if ( options.attempt == 1 ) {
locCB();
} else {
setTimeout(locCB, options.othOptions.gps.interval*1000);
}
}
return {
getCurrentLocation : function (options) {
var deferred = $q.defer();
callStart = new Date().getTime() / 1000;
_getLocationWrapper({callStart:callStart, deferred:deferred, othOptions:options, attempt:1});
return deferred.promise;
}
};
}])
直到几天前一直工作正常,当我尝试获取当前位置时,我收到以下错误:
message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 403.
任何人都知道如何让它发挥作用?
答案 0 :(得分:1)
每天有2500个请求限制。
https://developers.google.com/maps/faq#usagelimits
另一个原因可能是您使用的是旧版API。
如果您唯一需要的是地理位置,那么请使用此插件:
https://www.npmjs.com/package/cordova-plugin-geolocation
如果您需要更多内容,例如搜索,地图等,请使用此插件:
https://github.com/mapsplugin/cordova-plugin-googlemaps
<强>更新强>
如果您使用cordova-plugin-googlemaps,则非常容易。来自https://github.com/mapsplugin/cordova-plugin-googlemaps/wiki/Map的文档(获取我的位置):
var onSuccess = function(location) {
var msg = ["Current your location:\n",
"latitude:" + location.latLng.lat,
"longitude:" + location.latLng.lng,
"speed:" + location.speed,
"time:" + location.time,
"bearing:" + location.bearing].join("\n");
map.addMarker({
'position': location.latLng,
'title': msg
}, function(marker) {
marker.showInfoWindow();
});
};
var onError = function(msg) {
alert("error: " + msg);
};
map.getMyLocation(onSuccess, onError);