AngularJs 利用百度地图API 定位当前位置 获取地址信息

第一、申请百度密钥 很简单的几步就搞定

第二、引入文件

rush:xhtml;">

第三、绑定数据到你要显示的输入框内

rush:xhtml;"> 完整地址 所处城市: 所处区域: 所处街道:

第四、控制器中代码

rush:js;"> 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("定位失败,地址解析失败"); } }); }; } ]);

第五、完整代码如下:(大体思路就是这样!这里做个标记留给以后的自己)

rush:xhtml;"> <Meta charset="utf-8">

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程之家!

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...