Path: blob/trunk/javascript/atoms/html5/location.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Atom to retrieve the physical location of the device.19*20*/2122goog.provide('bot.geolocation');2324goog.require('bot');25goog.require('bot.html5');262728/**29* Default parameters used to configure the geolocation.getCurrentPosition30* method. These parameters mean retrieval of any cached position with high31* accuracy within a timeout interval of 5s.32* @const33* @type {!GeolocationPositionOptions}34* @see http://dev.w3.org/geo/api/spec-source.html#position-options35*/36bot.geolocation.DEFAULT_OPTIONS = /** @type {!GeolocationPositionOptions} */ ({37enableHighAccuracy: true,38maximumAge: Infinity,39timeout: 500040});414243/**44* Provides a mechanism to retrieve the geolocation of the device. It invokes45* the navigator.geolocation.getCurrentPosition method of the HTML5 API which46* later callbacks with either position value or any error. The position/47* error is updated with the callback functions.48*49* @param {function(?GeolocationPosition)} successCallback The callback method50* which is invoked on success.51* @param {function(?GeolocationPositionError)=} opt_errorCallback The callback52* method which is invoked on error.53* @param {?GeolocationPositionOptions=} opt_options The optional parameters to54* navigator.geolocation.getCurrentPosition; defaults to55* bot.geolocation.DEFAULT_OPTIONS.56*/57bot.geolocation.getCurrentPosition = function(successCallback,58opt_errorCallback, opt_options) {59var win = bot.getWindow();60var posOptions = opt_options || bot.geolocation.DEFAULT_OPTIONS;6162if (bot.html5.isSupported(bot.html5.API.GEOLOCATION, win)) {63var geolocation = win.navigator.geolocation;64geolocation.getCurrentPosition(successCallback,65opt_errorCallback, posOptions);66} else {67throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR, 'Geolocation undefined');68}69};707172