国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJs 利用百度地圖API 定位當前位置 獲取地址信息

2019-11-19 17:52:38
字體:
來源:轉載
供稿:網友

第一、申請百度密鑰  很簡單的幾步就搞定

第二、引入文件

<!-- 百度地圖定位 --><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>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梁山县| 新源县| 泽普县| 台山市| 台前县| 望城县| 隆尧县| 靖西县| 兴海县| 香港 | 章丘市| 波密县| 靖远县| 鹤庆县| 孟州市| 阿瓦提县| 大化| 自治县| 鹰潭市| 聂荣县| 抚远县| 丹寨县| 平顶山市| 潢川县| 抚宁县| 肥西县| 蒲江县| 白水县| 双牌县| 通榆县| 股票| 厦门市| 右玉县| 出国| 磴口县| 樟树市| 响水县| 含山县| 合阳县| 富顺县| 海盐县|