百度定位模块
最后更新于:2022-04-01 23:48:18
# 百度定位模块
> 引入第三方模块 **baiduLocation**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/bd4190ec98927dd7855b43d958ade0b7_508x100.png)
配置**apikey**, **ios** 和 **安卓**的AK均可
需要引入**init.js** 、**app.js** 、**api.js**
~~~
baiduLocation = api.require('baiduLocation');
baiduLocation.startLocation({
accuracy: '100m',
filter: 1,
autoStop: true
}, function(ret, err) {
if (ret.status) {
location2address(ret.latitude,ret.longitude);
} else {
alert(JSON.stringify(err));
}
});
function location2address(lat,lon)
{
var baidu_url="http://api.map.baidu.com/geocoder/v2/";//百度服务端接口地址
var data = "{values :{ak:'eUdorifLUUXVV9FMINfsTbTx',location:'"+lat+","+lon+"',output:'json',pois:'0'}}";
r_ajax(baidu_url,data,function(ret)
{
// alert($api.jsonToStr(ret));
cityName = ret.result.addressComponent.city; // 城市名称
cityName = cityName.replace("\"","").replace("\"",""); //正则去掉双引号
cityName = cityName.substring(0,cityName.length-1); //去掉最后一个市字
// alert(cityName);
$api.html($api.byId('cityname'),cityName);
});
}
~~~
';