AngularJs如何利用百度地圖API定位當(dāng)前位置獲取地址信息

這篇文章主要介紹AngularJs如何利用百度地圖API 定位當(dāng)前位置獲取地址信息,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站專(zhuān)注于威寧網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供威寧營(yíng)銷(xiāo)型網(wǎng)站建設(shè),威寧網(wǎng)站制作、威寧網(wǎng)頁(yè)設(shè)計(jì)、威寧網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造威寧網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供威寧網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

第一、申請(qǐng)百度密鑰  很簡(jiǎn)單的幾步就搞定

第二、引入文件

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

第三、綁定數(shù)據(jù)到你要顯示的輸入框內(nèi)

完整地址:<input type="text" ng-model="all"/><br>
所處城市:<input type="text" ng-model="shi"/><br>
所處區(qū)域:<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; 
       // 根據(jù)坐標(biāo)得到地址描述  
       $scope.getGeo(); 
      }  
     },{ 
      // 指示瀏覽器獲取高精度的位置,默認(rèn)為false 
      enableHighAccuracy: true, 
      // 指定獲取地理位置的超時(shí)時(shí)間,默認(rèn)不限時(shí),單位為毫秒 
      // timeout: 5000, 
      // 最長(zhǎng)有效期(30S),在重復(fù)獲取地理位置時(shí),此參數(shù)指定多久再次獲取位置 
      maximumAge: 30*1000 
     }); 
   }; 
  $scope.getGeo = function() {
  var myGeo = new BMap.Geocoder();
  // 根據(jù)坐標(biāo)得到地址描述
  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("定位失敗,地址解析失敗");
   }
  });
  };
  } ]);

第五、完整代碼如下:(大體思路就是這樣!這里做個(gè)標(biāo)記留給以后的自己)

<!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()'>點(diǎn)擊定位</button><br>
完整地址:<input type="text" ng-model="all"/><br>
所處城市:<input type="text" ng-model="shi"/><br>
所處區(qū)域:<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; 
  // 根據(jù)坐標(biāo)得到地址描述  
  $scope.getGeo(); 
  }  
  },{ 
  // 指示瀏覽器獲取高精度的位置,默認(rèn)為false 
  enableHighAccuracy: true, 
  // 指定獲取地理位置的超時(shí)時(shí)間,默認(rèn)不限時(shí),單位為毫秒 
  // timeout: 5000, 
  // 最長(zhǎng)有效期(30S),在重復(fù)獲取地理位置時(shí),此參數(shù)指定多久再次獲取位置 
  maximumAge: 30*1000 
  }); 
  }; 
  $scope.getGeo = function() {
  var myGeo = new BMap.Geocoder();
  // 根據(jù)坐標(biāo)得到地址描述
  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>

以上是“AngularJs如何利用百度地圖API 定位當(dāng)前位置獲取地址信息”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)題目:AngularJs如何利用百度地圖API定位當(dāng)前位置獲取地址信息
網(wǎng)站鏈接:http://bm7419.com/article14/pcohde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、服務(wù)器托管、Google、網(wǎng)站改版、虛擬主機(jī)、軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名