第一、申請百度密鑰 很簡單的幾步就搞定
第二、引入文件
<!-- 百度地圖定位 --><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>以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答