您当前的位置: 首页 > 

wespten

暂无认证

  • 1浏览

    0关注

    899博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

高德地图API的开发与代码编写

wespten 发布时间:2018-11-22 06:08:37 ,浏览量:1

高德地图API的开发与代码编写

高德地图底层源码

/** 高德autonavi 地图API Define:begin */
var AutoNaviMap = function(mapOption){
	
	this.map = null;													//高德autonavi地图对象
	
	this.mapWrap = mapOption.mapWrap;									//地图容器
	
	this.mapRange = mapOption.mapRange;									//地图范围
	
	this.centerPoint = mapOption.centerPoint;							//中心点
	
	this.GeoServerLayers = mapOption.GeoServerLayers;					//GeoServer 图层参数
	
	this.GeoServerlayerEntities = null;									//GeoServer 图层实例对象
	
	this.zoom = mapOption.zoom==undefined ? 15 : mapOption.zoom;
	
	this.contextMenuPositon;											//右键菜单位置
	
	this.mapLayer;//管线图层
	
	this.heatmap;
	
	this.toolBar = null; // 地图oolBar插件
	
	this.markers = [];
	
	this.infoWindow = {};													//global infoWindow
	
	this.polygon;
	
	this.init = function(){
		debugger;
		var self = this;
		if(!self.map){ 
			var option = {resizeEnable : true,								//监控地图容器尺寸变化,默认为false
							zoom : self.zoom, 							    //显示级别
							mapStyle: "amap://styles/darkblue",				//地图样式主题
							features:['bg', 'point', 'building'],			//地图展示元素
							logo : false};
			if(self.centerPoint && !$.isEmptyObject(self.centerPoint)){			//设置中心点
				var transCoordinate = {
						'lng':self.centerPoint.x,
						'lat':self.centerPoint.y
				};
				
				if(!mapOption.noTransAmap) {
					self.centerPoint.lng = self.centerPoint.x;
					self.centerPoint.lat = self.centerPoint.y;
					transCoordinate = self.get_GCJLngLat(self.centerPoint);
        		}
				option['center'] = new AMap.LngLat(transCoordinate.lng, transCoordinate.lat);
			}
			
			self.map = new AMap.Map(self.mapWrap, option);
			
			//地图单击事件
			if(mapOption.click && $.isFunction(mapOption.click)) {
				debugger;
				self.map.on('click',function(e){
					var mapPoint = getMapPoint(e);			//经纬度
					var screenPoint = getScreenPoint(e);
						mapOption.click(mapPoint, screenPoint);
				});  //点事件的获取
			}
			
			
			//地图缩放结束事件
			if(mapOption.zoomend && $.isFunction(mapOption.zoomend)) {
				self.map.on('zoomend',function(e){
						mapOption.zoomend();
				});  //点事件的获取
			}
			
			self.map.plugin(["AMap.ToolBar"], function() {
				toolBarOption = {
					offset:new AMap.Pixel(10,50)
				}
				toolBar = new AMap.ToolBar(toolBarOption);  
				self.map.addControl(toolBar);
			});
			//添加比例尺控件
			self.map.plugin(["AMap.Scale"],function(){
			    self.map.addControl(new AMap.Scale());  
			});
			//地图类型切换
			self.map.plugin(["AMap.MapType"],function(){	
			    var type= new AMap.MapType({
			    	defaultType:0 //使用2D地图
			    });
			    self.map.addControl(type);
			});
			
			$(".amap-maptype-wrap").bind('click',function(){
				if(self.mapLayer && self.map){
					self.mapLayer.setMap(self.map);
				}
			}); // 绑定地图切换插件单击事件
			
			
		}
		//在高德地图上 叠加 标准wms图层
		setTimeout(function(){
			if(self.GeoServerLayers && self.GeoServerLayers.length>0){
				self.GeoServerlayerEntities = {};
				for(var i=0,len=self.GeoServerLayers.length; i 0) {
			$.each(mPolygon.points, function(coordinateIndex, coordinateEntry) {
				if(coordinateEntry.lng && coordinateEntry.lat && Number(coordinateEntry.lng)>0 && Number(coordinateEntry.lat)>0){
	        		var transCoordinate = {
	        			'lng':coordinateEntry.lng,
	        			'lat':coordinateEntry.lat	
	        		}
	        		//若不是从天地图转换为高德地图,则不用转换坐标系
	        		if(!mPolygon.noTransAmap) {
	        			var transCoordinate = _self.get_GCJLngLat(coordinateEntry);
	        		}
		            coordinatesList.push(new AMap.LngLat(transCoordinate.lng, transCoordinate.lat));
	        	}
			});
			if(coordinatesList.length>0) {
				polygon = new AMap.Polygon({
					map:_self.map,
			        path: coordinatesList,//设置多边形边界路径
			        strokeColor: mPolygon.color?mPolygon.color:"#FF33FF", //线颜色
			        strokeOpacity: mPolygon.opacity?mPolygon.opacity:0.2, //线透明度
			        strokeWeight: mPolygon.width?mPolygon.width:3,    //线宽
			        fillColor: mPolygon.fillColor?mPolygon.fillColor:"#1791fc", //填充色
			        fillOpacity: mPolygon.fillOpacity?mPolygon.fillOpacity:0.35,//填充透明度
			        zIndex:mPolygon.zIndex
			    });
	        }
		}
		return polygon;
	};
	
	this.drawLine = function(mLine){
		var _self = this;
		var polyLine;
		var coordinatesList = [];
	    if (mLine.pionts.length > 0) {
	        $.each(mLine.pionts, function(coordinateIndex, coordinateEntry) {
	        	if(coordinateEntry.lng && coordinateEntry.lat && Number(coordinateEntry.lng)>0 && Number(coordinateEntry.lat)>0){
	        		var transCoordinate = {
	        			'lng':coordinateEntry.lng,
	        			'lat':coordinateEntry.lat	
	        		}
	        		//若不是从天地图转换为高德地图,则不用转换坐标系
	        		if(!mLine.noTransAmap) {
	        			var transCoordinate = _self.get_GCJLngLat(coordinateEntry);
	        		}
		            coordinatesList.push(new AMap.LngLat(transCoordinate.lng, transCoordinate.lat));
	        	}
	        });
	        if(coordinatesList.length>0) {
	        	polyLine = new AMap.Polyline({
	        		id : mLine.id,
	        		map:_self.map,
	        		path: coordinatesList,          //设置线覆盖物路径
	        		strokeColor: mLine.color, //线颜色
	        		strokeOpacity: mLine.opacity,       //线透明度
	        		strokeWeight: mLine.width,        //线宽
	        		strokeStyle: mLine.style,   //线样式
	        		strokeDasharray: mLine.dasharray //补充线样式
	        	});
	        }
	        
	        if(mLine.infowindow && !$.isEmptyObject(mLine.infowindow)) {
	        	if(!$('body').find('#infowindow').length>0) {
	        		var html = '

'; $('body').append(html); } polyLine.on('mouseover',function(e) { $('body').find('#infowindow p').html(mLine.infowindow.content); $('#infowindow').show(); $('#infowindow').css("left",e.pixel.x-$('#infowindow').width()/2); $('#infowindow').css("top",e.pixel.y-$('#infowindow').height()); }); polyLine.on('mouseout',function(e) { $('#infowindow').hide(); }); } if(mLine.onClick){ polyLine.on('click',function(e) { mLine.onClick(e); }); } } return polyLine; }; /** * 删除点 * point 为map地图对象, drawPoint时返回 */ this.removePoint = function(point){ this.map.remove(point); }; this.removeLine = function(mLine){ var _self = this; _self.map.remove(mLine); }; this.removePolygon = function(polygon){ var _self = this; _self.map.remove(polygon); }; this.drawPoint = function(mPoint){ debugger; var _self = this; var transCoordinate = { 'lng':mPoint.lng, 'lat':mPoint.lat }; var marker; if(mPoint.lng && mPoint.lat && Number(mPoint.lng)>0 && Number(mPoint.lat)>0){ //若不是从天地图转换为高德地图,则不用转换坐标系 if(!mPoint.noTransAmap) { transCoordinate = _self.get_GCJLngLat(mPoint); } var icon; if(mPoint.type.imgUrl == "") { icon = new AMap.Icon({ imageSize:new AMap.Size(mPoint.type.width,mPoint.type.height), size: new AMap.Size(mPoint.type.width,mPoint.type.height), //图标大小 }); } else { icon = new AMap.Icon({ imageSize:new AMap.Size(mPoint.type.width,mPoint.type.height), size: new AMap.Size(mPoint.type.width,mPoint.type.height), //图标大小 image:mPoint.type.imgUrl }); } var pointOpitons = { map:_self.map, icon:icon, position:[transCoordinate.lng,transCoordinate.lat], offset:new AMap.Pixel(mPoint.type.xoffset, mPoint.type.yoffset), //偏移量 title:mPoint.type.title, //点的文字提示 extData:mPoint }; marker = new AMap.Marker(pointOpitons); mPoint.marker = marker; if(mPoint.type.onClick && $.isFunction(mPoint.type.onClick)) { marker.on('click',function(e){ debugger; mPoint.type.onClick(e.target.getExtData(),null); }); //点事件的获取 } if(null != mPoint.labelContent && '' != mPoint.labelContent && 'undefined' != mPoint.labelContent){ marker.setLabel({ offset: new AMap.Pixel(20, -10), //显示位置 content: mPoint.labelContent //显示内容 }); } _self.markers.push(mPoint); } if(mPoint.type.showInfo && mPoint.type.infowindow && !$.isEmptyObject(mPoint.type.infowindow)) { debugger; if(marker != undefined) { var position = { 'lng':mPoint.lng, 'lat':mPoint.lat }; if(!mapOption.noTransAmap) { position = _self.get_GCJLngLat({'lng':mPoint.lng,'lat':mPoint.lat}); } var infowindow = mPoint.type.infowindow; var infoMouseOverWin = new AMap.InfoWindow({ isCustom:true, content: infowindow.content, offset: new AMap.Pixel(infowindow.xoffset,infowindow.yoffset) }); infoMouseOverWin.open(_self.map, new AMap.LngLat(position.lng,position.lat)); _self.infoWindow.uniqueCode = mPoint.busiKey; } } }; /** * 展示自定义弹出框 */ this.showInfoWindow = function(mPoint){ debugger; var _self = this; if(mPoint.type.infowindow && !$.isEmptyObject(mPoint.type.infowindow)) { var position = { 'lng':mPoint.lng, 'lat':mPoint.lat }; if(!mapOption.noTransAmap) { position = _self.get_GCJLngLat({'lng':mPoint.lng,'lat':mPoint.lat}); } var infowindow = mPoint.type.infowindow; var infoMouseOverWin = new AMap.InfoWindow({ isCustom:true, content: infowindow.content, offset: new AMap.Pixel(infowindow.xoffset,infowindow.yoffset) }); infoMouseOverWin.open(_self.map, new AMap.LngLat(position.lng,position.lat)); _self.infoWindow.uniqueCode = mPoint.busiKey; } } this.getInfoWindowUnique = function(){ var _self = this; return _self.infoWindow.uniqueCode; } this.clearGraphics = function(type) { var _self = this; for(var i =0; i 0) { $.each(heatMap.data, function(coordinateIndex, coordinateEntry) { if(coordinateEntry.lng && coordinateEntry.lat && Number(coordinateEntry.lng)>0 && Number(coordinateEntry.lat)>0){ var transCoordinate = { 'lng':coordinateEntry.lng, 'lat':coordinateEntry.lat } //若不是从天地图转换为高德地图,则不用转换坐标系 if(!heatMap.noTransAmap) { transCoordinate = _self.get_GCJLngLat(coordinateEntry); } coordinatesList.push(new AMap.LngLat(transCoordinate.lng, transCoordinate.lat)); } }); } if(coordinatesList.length>0) { _self.map.plugin(["AMap.Heatmap"], function() { //初始化heatmap对象 _self.heatmap = new AMap.Heatmap(_self.map, { radius: 80, //给定半径 opacity: [0, 0.8] }); //设置数据集 _self.heatmap.setDataSet({ data: coordinatesList, max: 2 }); }); } } /** *画圆 获取中心点及半径 *20180122 */ this.draw =function (){ var _self = this; var mouseTool = new AMap.MouseTool(_self.map); //自定义图形样式 mouseTool.polyline({ strokeColor:"#f50", // 线条颜色,十六进制 strokeOpacity:0.5, // 线条透明度 strokeWeight:10, // 线条宽度 strokeStyle:"dashed" // 线条样式 solid || dashed }); //在地图中添加MouseTool插件 mouseTool.circle(); //用鼠标工具画圆 AMap.event.addListener( mouseTool,'draw',function(e){ //添加事件 console.log(e.obj.getRadius());//获取半径 console.log(e.obj.getCenter());//获取中心点 showChoseDevice(e.obj.getRadius(),e.obj.getCenter()); mouseTool.close(true);//关闭当前鼠标操作 }); } ; //画圆 this.drawCircle = function(circle){ var _self = this; var center = new AMap.LngLat(circle.center.lng,circle.center.lat); var circleOptions = { center:center, zIndex:1000, radius:circle.radius, //半径1公里 strokeColor: "#F33", //线颜色 strokeOpacity: 1, //线透明度 strokeWeight: 3, //线粗细度 fillColor: "#ee2200", //填充颜色 fillOpacity: 0.35//填充透明度 }; var _circle = new AMap.Circle(circleOptions); _circle.setMap(_self.map); circle.marker = _circle; } this.circleContains = function(circle,point) { var _self = this; var transCoordinate = { 'lng':point.lng, 'lat':point.lat } //若不是从天地图转换为高德地图,则不用转换坐标系 if(!point.noTransAmap) { transCoordinate = _self.get_GCJLngLat(point); } var _point = new AMap.LngLat(transCoordinate.lng,transCoordinate.lat); var isContains = circle.marker.contains(_point); return isContains; } //绑定地图缩放事件 this.bindZoomChange = function(callBack) { var _self = this; _self.map.on('zoomchange', function(e) { callBack(_self.map.getZoom()); }); } //绑定地图单击事件 this.bindClick = function(callBack) { var _self = this; _self.map.on('click', function(e) { callBack(e.lnglat.getLng(),e.lnglat.getLat(),_self.map.getZoom()); }); } /** * 获取缩放等级 */ this.getZoom = function(){ var _self = this; return _self.map.getZoom(); } //显示或隐藏热力图 this.showHeatMap = function(isShow) { var _self = this; if(_self.heatmap) { if(isShow) { _self.heatmap.show(); } else { _self.heatmap.hide(); } } } //添加右键菜单获取坐标 this.addGetLnglatMenu = function() { var _self = this; var menu = new AMap.ContextMenu(); //添加菜单项 menu.addItem("发送任务",function() { debugger; mapDbClick(contextMenuPositon.getLat(),contextMenuPositon.getLng()); //alert(contextMenuPositon.getLng() + "," + contextMenuPositon.getLat()); },0); //为地图注册rightclick事件获取鼠标点击出的经纬度坐标 var clickEventListener = _self.map.on('rightclick', function(e) { menu.open(_self.map,e.lnglat); contextMenuPositon = e.lnglat; }); }; this.setCenter = function(lnt,lat) { var transCoordinate = { 'lng':lnt, 'lat':lat }; var _self = this; if(!mapOption.noTransAmap) { _self.centerPoint.lng = lnt; _self.centerPoint.lat = lat; transCoordinate = _self.get_GCJLngLat(_self.centerPoint); } _self.map.setCenter(new AMap.LngLat(transCoordinate.lng, transCoordinate.lat)); }; this.closeInfoWindow = function(infoWindow) { var _self = this; _self.map.clearInfoWindow(); }; this.fromLngLatToContainerPixel = function(mPoint) { var _self = this; var retObj = _self.map.lnglatToPixel(new AMap.LngLat(mPoint.lng,mPoint.lat)); return new SreenPoint(retObj); }; this.fromContainerPixelToLngLat = function(mPoint) { var _self = this; var retObj = _self.map.pixelToLngLat(new AMap.Pixel(mPoint.x,mPoint.y)); return new MapPoint(retObj); }; this.setFitView = function() { var _self = this; _self.map.setFitView(); }; this.zoomOut = function() { var _self = this; _self.map.zoomOut(); } this.get_GCJLngLat = function(mPoint) { var retObj = {}; var CT = new CoordinateTransform(mPoint.lat, mPoint.lng, CoordinateSys.WGS84);//CoordinateSys.WGS84 是转换前的坐标系 var retValue = new Object(); if (CT.WGS84ToGCJ02()== false)//WGS84ToBD09()方法 WGS84转BD坐标,如果是转成火星坐标改调用WGS84ToGCJ02() console.info(CT.get_ErrMsg()); retObj.lat = CT.get_GCJLat() == 0 ? mPoint.lat : CT.get_GCJLat();//获取转换后的高德坐标纬度 retObj.lng = CT.get_GCJLng() == 0 ? mPoint.lng : CT.get_GCJLng();//获取转换后的高德坐标精度 return retObj; }; }; function openWindowTask(tltle,url){ var options = { modal : true, title : tltle, collapsible : false, minimizable : false, maximizable : false, closable : true, closed : false }; var uid = "self_card_"; options["id"] = uid; winFormDesigner = UIFactory.getUI(uid); if(!winFormDesigner){ winFormDesigner = UIFactory.create(xpad.ui.Window, options); } var root = jQuery("body"); var offset = root.offset(); var winleft = 0; var wintop = 0; var newSize = {}; newSize["left"] = (jQuery("body").width()-450)/2; newSize["top"] = 20; newSize["width"] = 450;//jQuery("body").width(); newSize["height"] = 500;//jQuery("body").height(); winFormDesigner.window("resize", newSize); setTimeout(function(){ winFormDesigner.loadURL(url); }, 0); } AutoNaviMap.CLASS_NAME = "AutoNaviMap"; /**高德autonavi 地图API Define:end */

 

引入高德地图

var MyMap = function(mapOption){
	this.map = new mapOption.mapClass(mapOption);		//地图对象
	this.map.init();										//初始化地图组件
	
	/**
	 * 画线
	 */
	this.drawLine = function(line){
		return this.map.drawLine(line);
	};
	
	/**
	 * 画点
	 */
	this.drawPoint = function(point){
		return this.map.drawPoint(point);
	};
};
MyMap.CLASS_NAME = "MyMap";

$(function(){
	var mapOption = {
			'mapWrap' : 'pipeMap',									//地图容器
			/*'mapRange' : {'xmin':70.45193,'ymin':15.14801,
						  'xmax':137.424587,'ymax':53.976474},		//坐标范围
*/			'centerPoint' : {'x':116.383105, 'y':39.974672},		//中心点
			'mapClass' : AutoNaviMap,								//地图组件
			'isAddNavigationControl':true,                  		//工具条
			'isAddControl':false,					        		//类别切换
			'zoom': 10,
            'zoomend': function(mapPoint, screenPoint){
            	controlLayers(cntenMap.getZoom());
            },
			openSatelliteLayer:false
		};
	myMap = new MyMap(mapOption);
})

 

实现自己业务的代码,画点画线与信息框

$(function(){
	var mapOption = {
			'mapWrap' : 'leakMap',									//地图容器
			/*'mapRange' : {'xmin':70.45193,'ymin':15.14801,
						  'xmax':137.424587,'ymax':53.976474},		//坐标范围
*/			'centerPoint' : {'x':116.383105, 'y':39.974672},		//中心点
			'mapClass' : AutoNaviMap,								//地图组件
			'isAddNavigationControl':true,                  		//工具条
			'isAddControl':false,					        		//类别切换
			'zoom': 10,
            'zoomend': function(mapPoint, screenPoint){
            	controlLayers(cntenMap.getZoom());
            },
            'click' : function(mapPoint, screenPoint){				//地图点击事件
				console.log('地图坐标:(' + mapPoint.lng + ', ' + mapPoint.lat +');屏幕坐标:(' + screenPoint.x + ',' + screenPoint.y + ')');
			},
			openSatelliteLayer:false
		};
	myMap = new MyMap(mapOption);
})
//所有已绘制的线和点
var lines = [],drawPoints = [];
var pipe= {
	
		/**
		 * 画线
		 */
		drawLine:function(points){
			if(!$.isEmptyArray(lines)){
				myMap.removeLine(lines[0]);
			}
			//画线
			var line = {
					'color' : '#FF0033',
					'width' : 3,
					pionts:points,
				};
			
			lineObj = myMap.drawLine(line);
			lines.push(lineObj);
		},
		
		/**
		 * 画点
		 */
		drawPoint:function(point){
			myMap.drawPoint(point);
			drawPoints.push(point);
		},
		
		/**
		 * 删除点
		 */
		removePoint:function(){
			if(!$.isEmptyArray(drawPoints)){
				for(var i=0;i 0) {
	        $.each(mLine.pionts, function(coordinateIndex, coordinateEntry) {
	        	if(coordinateEntry.lng && coordinateEntry.lat && Number(coordinateEntry.lng)>0 && Number(coordinateEntry.lat)>0){
	        		//若不是从天地图转换为高德地图,则不用转换坐标系
	        		var transCoordinate = coordinateEntry;
	        		if(!mLine.noTransAmap) {
	        			transCoordinate = _self.get_GCJLngLat(coordinateEntry);
	        		}
		            coordinatesList.push(new AMap.LngLat(transCoordinate.lng, transCoordinate.lat));
	        	}
	        });
	        if(coordinatesList.length>0) {
	        	polyLine = new AMap.Polyline({
	        		map:_self.map,
	        		path: coordinatesList,          //设置线覆盖物路径
	        		strokeColor: mLine.color, //线颜色
	        		strokeOpacity: mLine.opacity,       //线透明度
	        		strokeWeight: mLine.width,        //线宽
	        		strokeStyle: mLine.style,   //线样式
	        		strokeDasharray: mLine.dasharray //补充线样式
	        	});
	        }
	        
	        if(mLine.infowindow && !$.isEmptyObject(mLine.infowindow)) {
				/*var infowindow = new AMap.InfoWindow({
					content:mLine.infowindow.content,
					offset:new AMap.Pixel(mLine.infowindow.xoffset,mLine.infowindow.yoffset),
					size:new AMap.Size(mLine.infowindow.width,mLine.infowindow.height)
				});*/
	        	if(!$('body').find('#infowindow').length>0) {
	        		var html = '

'; $('body').append(html); } polyLine.on('mouseover',function(e) { // infowindow.open(_self.map,e.target.getPosition()); $('body').find('#infowindow p').html(mLine.infowindow.content); $('#infowindow').show(); //获取地图容器在浏览器中的偏移 var offset = $('#'+ mapWrap).offset(); $('#infowindow').css("left",e.pixel.x-$('#infowindow').width()/2 + offset.left); $('#infowindow').css("top",e.pixel.y-$('#infowindow').height() + offset.top); }); polyLine.on('mouseout',function(e) { // infowindow.close(); $('#infowindow').hide(); }); } } return polyLine; }; this.drawPoint = function(mPoint){ debugger; var _self = this; var transCoordinate = mPoint; //若不是从天地图转换为高德地图,则不用转换坐标系 if(!mPoint.noTransAmap) { transCoordinate = _self.get_GCJLngLat(mPoint); } var marker = {}; if(mPoint.lng && mPoint.lat && Number(mPoint.lng)>0 && Number(mPoint.lat)>0){ var icon = new AMap.Icon({ size : new AMap.Size(mPoint.type.width,mPoint.type.height), imageSize:new AMap.Size(mPoint.type.width,mPoint.type.height), image:mPoint.type.imgUrl }); var pointOpitons = { map:_self.map, icon:icon, position:[transCoordinate.lng,transCoordinate.lat], offset:new AMap.Pixel(mPoint.type.xoffset, mPoint.type.yoffset), //偏移量 title:mPoint.type.title, //点的文字提示 extData:mPoint }; marker = new AMap.Marker(pointOpitons); if(mPoint.type.onClick && $.isFunction(mPoint.type.onClick)) { marker.on('click',function(e){ debugger; mPoint.type.onClick(e.target.getExtData(),e); }); //点事件的获取 } } if(mPoint.type.infowindow && !$.isEmptyObject(mPoint.type.infowindow)) { /*var infowindow = new AMap.InfoWindow({ content:mPoint.type.infowindow.content, offset:new AMap.Pixel(mPoint.type.infowindow.xoffset,mPoint.type.infowindow.yoffset), size:new AMap.Size(mPoint.type.infowindow.width,mPoint.type.infowindow.height) });*/ if(!$('body').find('#infowindow').length>0) { var html = '

'; $('body').append(html); } marker.on('mouseover',function(e) { // infowindow.open(_self.map,e.target.getPosition()); $('body').find('#infowindow p').html(mPoint.type.infowindow.content); var tmpPixel = _self.map.lngLatToContainer(e.target.getPosition()); $('#infowindow').show(); //获取地图容器在浏览器中的偏移 var offset = $('#'+ mapWrap).offset(); $('#infowindow').css("left",tmpPixel.x-$('#infowindow').width()/2 + offset.left); $('#infowindow').css("top",tmpPixel.y-$('#infowindow').height() + offset.top - Math.abs(e.target.getOffset().y)); }); marker.on('mouseout',function(e) { // infowindow.close(); $('#infowindow').hide(); }); } return marker; }; this.fromLngLatToContainerPixel = function(mPoint) { var _self = this; var retObj = _self.map.lnglatToPixel(new AMap.LngLat(mPoint.lng,mPoint.lat)); return new SreenPoint(retObj); }; this.fromContainerPixelToLngLat = function(mPoint) { var _self = this; var retObj = _self.map.pixelToLngLat(new AMap.Pixel(mPoint.x,mPoint.y)); return new MapPoint(retObj); }; this.setFitView = function() { var _self = this; _self.map.setFitView(); }; this.get_GCJLngLat = function(mPoint) { var retObj = {}; var CT = new CoordinateTransform(mPoint.lat, mPoint.lng, CoordinateSys.WGS84);//CoordinateSys.WGS84 是转换前的坐标系 if (CT.WGS84ToBD09()== false)//WGS84ToBD09()方法 WGS84转BD坐标,如果是转成火星坐标改调用WGS84ToGCJ02() console.info(CT.get_ErrMsg()); retObj.lat = CT.get_GCJLat() == 0 ? mPoint.lat : CT.get_GCJLat();//获取转换后的高德坐标纬度 retObj.lng = CT.get_GCJLng() == 0 ? mPoint.lng : CT.get_GCJLng();//获取转换后的高德坐标精度 return retObj; }; /** * 设置中心点 * lng 纬度 * lat 经度 */ this.centerAt = function(lng, lat){ this.map.setCenter(new AMap.LngLat(lng, lat)); }; /** * 设置标记 */ this.markPoint = function(lng, lat){ // return this.map.Marker({ // position: new AMap.LngLat(lng, lat), // map: _self.map // }); return new AMap.Marker({ position: new AMap.LngLat(lng, lat), map: _self.map }); }; /** * 删除点 * point 为map地图对象, drawPoint时返回 */ this.removePoint = function(point){ this.map.remove(point); }; /** * 删除线 * line 为map地图对象, drawLine时返回 */ this.removeLine = function(line){ this.map.remove(line); }; /** * 设置级别 * zoom Number */ this.setZoom = function(zoom){ this.map.setZoom(zoom); }; /** * 获取可视区域 */ this.getBounds = function(){ //TODO 返回MapBounds 对象 var bounds = this.map.getBounds(); // alert('lng:' + bounds.xmin + ';lat:' + bounds.ymin); //左下角, 西南坐标 // alert('lng:' + bounds.xmax + ';lat:' + bounds.ymax); //右上角, 东北坐标 return new MapBounds(bounds.southwest.lng, bounds.southwest.lat, bounds.northeast.lng, bounds.northeast.lat); } /** * e 鼠标事件对象 */ function getScreenPoint(e){ return new SreenPoint({x:e.pixel.x, y:e.pixel.y}); }; /** * e 鼠标事件对象 */ function getMapPoint(e){ return new MapPoint({'lng' : e.lnglat.lng, 'lat' : e.lnglat.lat}); }; /** * 显示弹框 */ this.showInfoWindow = function(infoWindow){ var _self = this; _self.infoWindow = new AMap.InfoWindow({ content: infoWindow.content //使用默认信息窗体框样式,显示信息内容 }); _self.infoWindow.open(this.map, [infoWindow.lng, infoWindow.lat]); }; /** * 圈选覆盖物 */ this.selectOverlay = function(callBack){ var _self = this; AMap.plugin(["AMap.MouseTool"], function() { _self.map.setDefaultCursor("crosshair"); mousetool = new AMap.MouseTool(_self.map); //通过rectOptions更改拉框放大时鼠标绘制的矩形框样式 var rectOptions = { strokeStyle: "solid", strokeColor: "#FF0000", fillColor: "#C0C0C0", fillOpacity: 0.5, strokeOpacity: 1, strokeWeight: 2 }; mousetool.rectangle(rectOptions); AMap.event.addListener(mousetool ,"draw",function(e){ var curBounds = e.obj.getBounds(); var allPoints = _self.map.getAllOverlays("marker"); var resultMarker = []; for(var i = 0; i
关注
打赏
1665965058
查看更多评论
0.0449s