AngularJs 利用百度地圖API 定位當(dā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,
// 指定獲取地理位置的超時時間,默認(rèn)不限時,單位為毫秒
// timeout: 5000,
// 最長有效期(30S),在重復(fù)獲取地理位置時,此參數(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("定位失敗,地址解析失敗");
}
});
};
} ]);
第五、完整代碼如下:(大體思路就是這樣!這里做個標(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,
// 指定獲取地理位置的超時時間,默認(rèn)不限時,單位為毫秒
// timeout: 5000,
// 最長有效期(30S),在重復(fù)獲取地理位置時,此參數(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)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
Angular2使用Angular CLI快速搭建工程(一)
這篇文章主要介紹了Angular2使用Angular CLI快速搭建工程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Angular之jwt令牌身份驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Angular之jwt令牌身份驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Angular.js與Bootstrap相結(jié)合實(shí)現(xiàn)表格分頁代碼
最近一直在學(xué)習(xí)angularjs相關(guān)知識,在學(xué)習(xí)過程中寫了一個小demo,下面把代碼思路分享給大家,感興趣的朋友一起學(xué)習(xí)2016-04-04
淺析angularJS中的ui-router和ng-grid模塊
下面小編就為大家?guī)硪黄獪\析angularJS中的ui-router和ng-grid模塊。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
Angular應(yīng)用里環(huán)境變量SERVER_REQUEST_ORIGIN含義解析
這篇文章主要為大家介紹了Angular應(yīng)用里環(huán)境變量SERVER_REQUEST_ORIGIN的含義解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
AngularJS 中使用Swiper制作滾動圖不能滑動的解決方法
Swiper是目前較為流行的移動端觸摸滑動插件,因?yàn)槠浜唵魏糜靡咨鲜?,受到很多前端開發(fā)者的歡迎。這篇文章主要介紹了AngularJS 中使用Swiper制作滾動圖不能滑動的解決方法,需要的朋友可以參考下2016-11-11

