AngularJs 利用百度地图API 定位当前位置 获取地址信息
更新时间:2017年01月18日 15:56:36 作者:潘冬子
本文主要介绍了AngularJs 利用百度地图API 定位当前位置 获取地址信息的方法步骤。具有一定的参考价值,下面跟着小编一起来看下吧
第一、申请百度密钥 很简单的几步就搞定
第二、引入文件
<!-- 百度地图定位 --> <script src="http://api.map.baidu.com/components?ak=WUfZTjKPuZ2G5RmgD0Psejv6XOmIEQVQ"></script> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=WUfZTjKPuZ2G5RmgD0Psejv6XOmIEQVQ"></script>
第三、绑定数据到你要显示的输入框内
完整地址:<input type="text" ng-model="all"/><br> 所处城市:<input type="text" ng-model="shi"/><br> 所处区域:<input type="text" ng-model="qu"/><br> 所处街道:<input type="text" ng-model="jiedao"/>
第四、控制器中代码
angular.module('myApp')
.controller('myCtrl',function($scope) {
//获取地理位置信息
$scope.getAddr = function() {
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(
//获取位置信息成功
function(position){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
$scope.longitude = position.point.lng;
$scope.latitude = position.point.lat;
// 根据坐标得到地址描述
$scope.getGeo();
}
},{
// 指示浏览器获取高精度的位置,默认为false
enableHighAccuracy: true,
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
// timeout: 5000,
// 最长有效期(30S),在重复获取地理位置时,此参数指定多久再次获取位置
maximumAge: 30*1000
});
};
$scope.getGeo = function() {
var myGeo = new BMap.Geocoder();
// 根据坐标得到地址描述
myGeo.getLocation(new BMap.Point($scope.longitude,$scope.latitude),
function(result) {
if (result) {
$scope.geoaddress = {
'fulladdress' : result.addressComponents.city+ result.addressComponents.district+ result.addressComponents.street,
'city' : result.addressComponents.city,
'area' : result.addressComponents.district,
'street' : result.addressComponents.street,
};
$scope.all = result.addressComponents.city+ result.addressComponents.district+ result.addressComponents.street;
$scope.shi = result.addressComponents.city;
$scope.qu = result.addressComponents.district;
$scope.jiedao = result.addressComponents.street;
alert(JSON.stringify($scope.all))
} else {
$scope.showAlert("定位失败,地址解析失败");
}
});
};
} ]);
第五、完整代码如下:(大体思路就是这样!这里做个标记留给以后的自己)
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="http://api.map.baidu.com/components?ak=WUfZTjKPuZ2G5RmgD0Psejv6XOmIEQVQ"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=WUfZTjKPuZ2G5RmgD0Psejv6XOmIEQVQ"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<button type="button" ng-click='getAddr()'>点击定位</button><br>
完整地址:<input type="text" ng-model="all"/><br>
所处城市:<input type="text" ng-model="shi"/><br>
所处区域:<input type="text" ng-model="qu"/><br>
所处街道:<input type="text" ng-model="jiedao"/>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//获取地理位置信息
$scope.getAddr = function() {
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(
//获取位置信息成功
function(position){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
$scope.longitude = position.point.lng;
$scope.latitude = position.point.lat;
// 根据坐标得到地址描述
$scope.getGeo();
}
},{
// 指示浏览器获取高精度的位置,默认为false
enableHighAccuracy: true,
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
// timeout: 5000,
// 最长有效期(30S),在重复获取地理位置时,此参数指定多久再次获取位置
maximumAge: 30*1000
});
};
$scope.getGeo = function() {
var myGeo = new BMap.Geocoder();
// 根据坐标得到地址描述
myGeo.getLocation(new BMap.Point($scope.longitude,$scope.latitude),
function(result) {
if (result) {
$scope.geoaddress = {
'fulladdress' : result.addressComponents.city+ result.addressComponents.district+ result.addressComponents.street,
'city' : result.addressComponents.city,
'area' : result.addressComponents.district,
'street' : result.addressComponents.street,
};
$scope.all = result.addressComponents.city+ result.addressComponents.district+ result.addressComponents.street;
$scope.shi = result.addressComponents.city;
$scope.qu = result.addressComponents.district;
$scope.jiedao = result.addressComponents.street;
alert(JSON.stringify($scope.all))
} else {
$scope.showAlert("定位失败,地址解析失败");
}
});
};
});
</script>
</body>
</html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
相关文章
Angular2使用Angular CLI快速搭建工程(一)
这篇文章主要介绍了Angular2使用Angular CLI快速搭建工程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05
Angular.js与Bootstrap相结合实现表格分页代码
最近一直在学习angularjs相关知识,在学习过程中写了一个小demo,下面把代码思路分享给大家,感兴趣的朋友一起学习2016-04-04
浅析angularJS中的ui-router和ng-grid模块
下面小编就为大家带来一篇浅析angularJS中的ui-router和ng-grid模块。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-05-05
Angular应用里环境变量SERVER_REQUEST_ORIGIN含义解析
这篇文章主要为大家介绍了Angular应用里环境变量SERVER_REQUEST_ORIGIN的含义解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-10-10
AngularJS 中使用Swiper制作滚动图不能滑动的解决方法
Swiper是目前较为流行的移动端触摸滑动插件,因为其简单好用易上手,受到很多前端开发者的欢迎。这篇文章主要介绍了AngularJS 中使用Swiper制作滚动图不能滑动的解决方法,需要的朋友可以参考下2016-11-11


最新评论