定位,签到,转换坐标系
cordova-plugin-geolocation geolocation插件
1 安装插件
cordova plugin add cordova-plugin-geolocation
2 点击定位
<button ng-click="getGeolocation()">定位当前</button>
```
$scope.getGps=function(){
$cordovaGeolocation.getCurrentPosition().then(function (position) {
$scope.lat = position.coords.latitude;
$scope.lon = position.coords.longitude;
$http.jsonp("http://api.map.baidu.com/geocoder/v2/?callback=JSON_CALLBACK&mcode=com.xxxxx.-&location="+$scope.lat+","+$scope.lon+"&output=json&pois=1&ak=ysHscdqZCVZXG4ipXgXou2Wt")
.success(function(data){
$scope.formatted_address=data.result.formatted_address;
$scope.sematic_description=data.result.sematic_description;
console.log(data)
})
}, function(err) {
// error
});
}
```
签到功能:谷歌和百度地图利用的坐标系不一样,需要转换坐标系
```
// 签到功能
$scope.getGps=function(){
var posOptions = {timeout: 10000,maximumAge: 30000, enableHighAccuracy: true,coorType: 'bd09ll'};
$cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
$scope.lat = position.coords.latitude;
$scope.lon = position.coords.longitude;
bd_encrypt($scope.lat, $scope.lon)
$http.jsonp("http://api.map.baidu.com/geocoder/v2/?callback=JSON_CALLBACK&mcode=com.xxxxx.-&location="+$scope.lat+","+$scope.lon+"&output=json&pois=1&ak=ysHscdqZCVZXG4ipXgXou2Wt")
.success(function(data){
$scope.formatted_address=data.result.formatted_address;
$scope.sematic_description=data.result.sematic_description;
})
$.ajax({
type:"post",
url: loginIP + '/api/v1/lines',
// url:'/api/v1/lines',
headers:{"Authentication-Token": sessionNews},
data:{d_task_form_id:$scope.id,long_login:$scope.lon,lat_login:$scope.lat},
success:function(data){
console.log(data)
if(data.status == '定位成功'){
alert('签到成功')
}else{
alert('签到失败')
}
},
error:function(err){
$ionicLoading.hide()
console.log(err)
}
});
});
}
// 谷歌转百度
function bd_encrypt(gg_lon, gg_lat){
var X_PI = Math.PI * 3000.0 / 180.0;
var x = gg_lon, y = gg_lat;
var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
var bd_lon = z * Math.cos(theta) + 0.0065;
var bd_lat = z * Math.sin(theta) + 0.006;
return {
bd_lat: bd_lat,
bd_lon: bd_lon
};
}
```
网友评论