码迷,mamicode.com
首页 > 其他好文 > 详细

高德地图:定位、覆盖物

时间:2017-10-19 15:03:41      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:获取   erro   中心   new   color   ddl   scope   定位   str   

 

高德地图需登录需注册登录,在个人中心中添加项目,拿到认证的一串web id。

app.service("Map_g",function ($q) { this.getMessage=function (map) { //定位系统 
var defer = $q.defer();
map.plugin(‘AMap.Geolocation‘, function() {
geolocation = new AMap.Geolocation({ enableHighAccuracy: true,
timeout: 10000,
buttonOffset: new AMap.Pixel(10, 20),
zoomToAccuracy: false,
buttonPosition:‘RB‘ });
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, ‘complete‘, onComplete); AMap.event.addListener(geolocation, ‘error‘, onError); });
function onComplete(data) { defer.resolve(data); } function onError(data) { defer.reject(data); } return defer.promise; } ;
this.coverOperate=function (map,lang,lat,name) { //覆盖物
var marker = new AMap.Marker({}); marker.setMap(map);
var markerContent = document.createElement("div");
markerContent.className="markWrap"; var markerImg = document.createElement("img");
markerImg.className = "markerlnglat";
markerImg.src = "img/48.png";
markerContent.appendChild(markerImg);
var markerSpan = document.createElement("span");
markerSpan.className = ‘marker‘;
markerSpan.innerHTML = name; markerContent.appendChild(markerSpan);
marker.setContent(markerContent);
marker.setPosition([lang,lat]); }; });
$scope.getMap = function() { //定位开始,获取地理位置信息,拿到经纬度进行覆盖物操作。
var geolocation; $scope.map = new AMap.Map(‘allmap‘, { resizeEnable: true });
Map_g.getMessage($scope.map).then(function (data) {
if(data.addressComponent != undefined && data.addressComponent != null && data.addressComponent.district){
$scope.isOpenLocation = false; $scope.original = {lang:data.position.getLng(),lat:data.position.getLat()};
$scope.require(data.addressComponent.province,data.addressComponent.city,data.addressComponent.district);
$scope.headerName = data.addressComponent.district; $scope.$parent.district = data.addressComponent.district;
}else{ $scope.getAllShops(); $scope.isOpenLocation = true; } },function (data) { angular.element("#gif").hide();
$scope.getAllShops(); $scope.isOpenLocation = true; }); };
$scope.getMap(); //$scope.map = $scope.map = new AMap.Map(‘allmap‘, { resizeEnable: true});
//longitude ,latitude 经纬度 //name 覆盖物展示文字
Map_g.coverOperate($scope.map,val.longitude,val.latitude,val.name);
//调用函数实现覆盖物的操做。 //计算两地点的距离
var lnglat = new AMap.LngLat($scope.original.lang,$scope.original.lat); val.distance=parseInt(lnglat.distance([val.longitude,val.latitude])) + "米";

 

高德地图:定位、覆盖物

标签:获取   erro   中心   new   color   ddl   scope   定位   str   

原文地址:http://www.cnblogs.com/changyaoself/p/7692169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!