使用谷歌地图检索当前位置
Posted
技术标签:
【中文标题】使用谷歌地图检索当前位置【英文标题】:Retrieving Current location using Google Maps 【发布时间】:2016-01-13 04:57:22 【问题描述】:我的代码检查当前用户位置,然后使用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.
有人知道我怎样才能让它工作吗?
【问题讨论】:
【参考方案1】:每天的请求限制为 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 = ["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);
【讨论】:
感谢您的回复。我只需要地理定位,以便能够将它们放置在地图上。我也在使用最新版本的cordova-plugin-geolocation 让我试试你的方法。非常感谢您抽出宝贵时间提供帮助,我衷心感谢 我正在努力让它与离子框架一起工作:-( cordova-plugin-geolocation 是否正在运行?或者问题出在哪里? 我猜在哪里初始化。我正在将地图加载到控制器中,但不断收到插件未定义的错误。这是我的代码的 sn-p:jsfiddle.net/5db0y2cz以上是关于使用谷歌地图检索当前位置的主要内容,如果未能解决你的问题,请参考以下文章