适合移动端的 GeoLocation 封装
通过 edp 引入模块:
edp import saber-geo
require( [ 'saber-geo' ], function( Geo ) {
function onSuccess( position ) {
var coords = position.coords;
console.log( 'latitude,longitude: ', coords.latitude, coords.longitude);
}
function onError( error ) {
console.log( 'Error: ', error.code, error.message );
}
// 一次性获取
Geo.get( onSuccess, onError );
// 持续性获取
var watchId = Geo.watch( onSuccess, onError );
// 停止获取
Geo.clear( watchId );
});
该部分是标准的 HTML5 Geolocation API 封装,在 require( 'saber-geo' )
时加载。
一次性
获取当前地理位置信息
。
Geo.get(
function ( position ) {
console.info( 'Position: ', position );
},
function ( error ) {
console.error( 'Error: ', error );
}
);
- onSuccess
{Function}
成功回调,参数参考Position
- onError
{Function=}
错误回调,参数参考PositionError
- options
{Object=}
配置对象- highAcuracy
{boolean=}
是否使用高精度 - timeout
{number=}
超时时长,单位毫秒 - age
{number=}
数据缓存时间,单位毫秒,为0
时每次都执行新的检索
- highAcuracy
持续性
获取当前地理位置信息
。
Geo.watch(
function ( position ) {
console.info( 'Position: ', position );
},
function ( error ) {
console.error( 'Error: ', error );
},
{ timeout: 20000, age: 10000 }
);
- onSuccess
{Function}
成功回调,参数参考Position
- onError
{Function=}
错误回调,参数参考PositionError
- options
{Object=}
配置对象- highAcuracy
{boolean=}
是否使用高精度 - timeout
{number=}
超时时长,单位毫秒 - age
{number=}
数据缓存时间,单位毫秒,为0
时每次都执行新的检索
- highAcuracy
停止指定的位置监控。
var watchId = Geo.watch( ... );
Geo.clear( watchId );
- watchId
{number}
watch
方法返回的watchId
- Object IP模块
- require('saber-geo/ip')
在线IP查询服务
的扩展支持,需以 require( 'saber-geo/ip' )
引入。
- options
{Object}
- callback
{string}
查询请求(JSOP
)附带的callback
参数的键值
,默认为callback
- provider
{string}
查询服务的JSONP
服务地址,默认为http://hendless.duapp.com/addr
- callback
查询当前IP信息
- callback
{Function}
回调函数
require( [ 'saber-geo/ip' ], function( IP ) {
IP.find( function () {
console.info( 'IP Info: ', arguments );
});
});