使用Google地图检索当前位置 [英] Retrieving Current location using Google Maps

查看:153
本文介绍了使用Google地图检索当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码检查当前用户的位置,然后使用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});
},
函数(位置){
var callEnd = new Date()。getTime()/ 1000;
locWatch.clearWatch();
if(position.coords.accuracy&& position.coords.accuracy< = othOptions.gps.accuracy){
//这是很好的准确性,然后接受
deferred.resolve ({status:0,position:position}); $(b $ b)else if((callEnd - callStart)< othOptions.gps.timeout){
//继续尝试,直到配置的等待时间。如果超过,则返回。
options.attempt ++;
options.lastAccuracy = Math.round(position.coords.accuracy * 100)/ 100;
options.minAccuracy = options.minAccuracy || options.lastAccuracy; //默认
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>最精确度是 + options.minAccuracy +mts。< br>尝试检查空旷区域的位置或尝试调整到可接受的准确度。}});
}
}
);



$ b函数_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;
}
};
}])

直到前几天,我尝试获得当前位置我收到以下错误消息:

 消息:网络位置提供程序位于'https://www.googleapis.com /':返回的错误代码为403. 

任何人都知道我可以如何解决这个问题?

解决方案

每天有2500个请求限制。

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



另一个原因可能是,您使用的是Google的旧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 = [当前位置:\\\

latitude:+ location.latLng.lat,
longitude:+ location.latLng.lng,
speed :+ location.speed,
time:+ location.time,
bearing:+ location.bearing] .join(\\\
);

map.addMarker(
'position':location.latLng,
'title':msg
},function(marker){
marker .showInfoWindow();
});
};

var onError =函数(msg){
alert(error:+ msg);
};
map.getMyLocation(onSuccess,onError);


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?

解决方案

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

这篇关于使用Google地图检索当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆