AngularJs 利用百度地圖API 定位當(dāng)前位置 獲取地址信息
第一、申請(qǐng)百度密鑰 很簡單的幾步就搞定
第二、引入文件
<!-- 百度地圖定位 --> <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, // 最長有效期(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, // 最長有效期(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>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Angular實(shí)現(xiàn)類似博客評(píng)論的遞歸顯示及獲取回復(fù)評(píng)論的數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于Angular如何實(shí)現(xiàn)類似博客評(píng)論的遞歸顯示及獲取回復(fù)評(píng)論的數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11AngularJS實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實(shí)例
這篇文章給大家分享了AngularJS循環(huán)實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等基礎(chǔ)功能,對(duì)大家學(xué)習(xí)AngularJS具有一定的參考借鑒價(jià)值,有需要的朋友可以看看。2016-09-09Angular4自制一個(gè)市縣二級(jí)聯(lián)動(dòng)組件示例
本篇文章主要介紹了Angular4自制一個(gè)市縣二級(jí)聯(lián)動(dòng)組件示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Angular.js ng-file-upload結(jié)合springMVC的使用教程
這篇文章主要給大家介紹了關(guān)于Angular.js文件上傳控件ng-file-upload結(jié)合springMVC的使用教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07AngularJS基于MVC的復(fù)雜操作實(shí)例講解
下面小編就為大家分享一篇AngularJS基于MVC的復(fù)雜操作實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12基于AngularJS實(shí)現(xiàn)頁面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能
本文主要給大家介紹基于AngularJS實(shí)現(xiàn)頁面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能,通過第三方控件來實(shí)現(xiàn),感興趣的朋友跟著小編一起看看具體實(shí)現(xiàn)代碼吧2015-10-10AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解
這篇文章主要介紹了AngularJS基于factory創(chuàng)建自定義服務(wù)的方法,結(jié)合實(shí)例形式分析了AngularJS使用factory創(chuàng)建自定義服務(wù)的具體步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05