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>


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值