高德地图:定位覆盖物
Posted changYao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高德地图:定位覆盖物相关的知识,希望对你有一定的参考价值。
高德地图需登录需注册登录,在个人中心中添加项目,拿到认证的一串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])) + "米";
以上是关于高德地图:定位覆盖物的主要内容,如果未能解决你的问题,请参考以下文章