Masinde Muliro
Masinde Muliro

Reputation: 1183

Retrieving Current location using Google Maps

My code checks current user position and then loads all nearby stores including distance to location using cordova's geolocation plugin.

This is my code:

.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;
    }
};
  }])

It worked fine until a few days ago, when I try to get the current position I get the following error:

message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 403.

Anyone know how I can get this to work?

Upvotes: 0

Views: 2323

Answers (1)

Joerg
Joerg

Reputation: 3101

There is a request limit of 2500 per day.

https://developers.google.com/maps/faq#usagelimits

Another reason could be, that you are using an old API from Google.

If the only thing you need is the geolocation, then use this plugin:

https://www.npmjs.com/package/cordova-plugin-geolocation

If you need more things like searching, maps, …, then take this plugin:

https://github.com/mapsplugin/cordova-plugin-googlemaps

Update:

If you use cordova-plugin-googlemaps then it is very easy. From the docs at https://github.com/mapsplugin/cordova-plugin-googlemaps/wiki/Map (Get my location):

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);

Upvotes: 1

Related Questions