/**

* 仅获取定位(获取地址和经纬度)

*/

self.getAddr = function (callback) {

//判断平台(安卓平台采用高德定位,ios平台采用系统定位)amapLBS 在ios下定位非常慢

var os = app.device.os;

if (os == 'ios') {

//先调用系统定位获取经纬度

api.getLocation(function (ret, err) {

if (ret && ret.status) {

//再根据经纬度获取地址

geocode(ret.longitude, ret.latitude, callback);

} else {

callback(false);

}

});

} else if (os == 'android') {

common.aMapLBS(callback);

};

};

//ios 使用高德web服务,http://lbs.amap.com/api/webservice/guide/api/georegeo

function geocode(lon, lat, callback) {

var location = lon + ',' + lat;

var url = 'http://restapi.amap.com/v3/geocode/regeo?location=' + location + '&key=0c8be3011cbc3c0a39697e5220fb7529&radius=10';

api.ajax({

url: url,

method: 'get',

dataType: 'json',

cache: true

}, function (ret, err) {

if (ret && ret.status) {

var result = {};

result.attendanceAddr = ret.regeocode.formatted_address;

result.lon = lon;

result.lat = lat;

result.province = ret.regeocode.addressComponent.province;

result.city = ret.regeocode.addressComponent.city;

callback(result);

} else {

common.ajaxError(ret, err, null, '定位失败');

};

});

};

//目前仅用于安卓定位

self.aMapLBS = function (callback) {

//安卓平台也要返回经纬度和地址

var aMapLBS = api.require('aMapLBS');

aMapLBS.configManager({

accuracy: 'best',

filter: 1.0

}, function (ret, err) {

if (ret.status) {

// getLocation();

singleAddress()

} else {

console.log('请检查', '配置定位信息失败');

callback(false);

}

});

//地理定位

function getLocation(result) {

aMapLBS.startLocation(function (ret, err) {

if (ret && ret.status) {

//返回经纬度

result.lon = ret.lon;

result.lat = ret.lat;

callback(result)

} else {

callback(false);

}

aMapLBS.stopUpdatingLocation();

});

};

//逆地理定位

function singleAddress(locationResult) {

aMapLBS.singleAddress({

timeout: 10

}, function (ret, err) {

if (ret && ret.status) {

var result = ret.address;

//考勤地址(街道+地点)

result.attendanceAddr = result.street + result.AOIName + result.POIName;

//callback(result);

getLocation(result);

} else {

callback(false);

}

// aMapLBS.stopUpdatingLocation();

// aMapLBS = null;

});

};

};

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐