這篇文章主要介紹AngularJs如何利用百度地圖API 定位當前位置獲取地址信息,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
第一、申請百度密鑰 很簡單的幾步就搞定
第二、引入文件
<!-- 百度地圖定位 --> <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>
以上是“AngularJs如何利用百度地圖API 定位當前位置獲取地址信息”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。