之前想本身做一个旅游导航的项目,在网上一搜发明白高德舆图开放平台,发明本来高德可以很简朴的就应用到本身的项目内里,连忙我就申请了一个key来学一学,仔细研究了一下,感受还挺难的,网上找了找案例什么的,颠末这几天,小编把高德的一些基本控件差不多弄了一下,结果图如下图所示:
空话不多说,直接上源码,下面是js代码:
<script language="javascript" src="https://webapi.amap.com/maps?v=1.2&key=3c5ca12a5778fde874e9959c7fbdf516">//引入高德舆图API</script> <script language="javascript"> var mapObj; var scale; var mapType; var toolBar; var overView; var circleEditor; var circle; var polygonEditor; var polygon; var homeControl; var controlUI; var ruler; var mousetool; //刷新页面 function reload(){ location.reload(); } function mapInit(){ mapObj = new AMap.Map("iCenter",{ center:new AMap.LngLat(116.397728,39.90423), //舆图中心点 level:13, //舆图显示的比例尺级别 }); mapObj.plugin(["AMap.ToolBar"],function(){ //在舆图中添加ToolBar插件 toolBar = new AMap.ToolBar(); mapObj.addControl(toolBar); }); mapObj.plugin(["AMap.Scale"],function(){ //加载比例尺插件 scale = new AMap.Scale(); mapObj.addControl(scale); scale.show(); }); mapObj.plugin(["AMap.OverView"],function(){ //在舆图中添加鹰眼插件 //加载鹰眼 overView = new AMap.OverView({ visible:true //初始化显示鹰眼 }); mapObj.addControl(overView); overView.open(); //展开鹰眼 }); mapObj.plugin(["AMap.RangingTool"],function(){ ruler = new AMap.RangingTool(mapObj); AMap.event.addListener(ruler,"end",function(e){ ruler.turnOff(); }); }); mapObj.plugin(["AMap.MouseTool"],function(){ //鼠标东西插件 mousetool = new AMap.MouseTool(mapObj); }); } function Coordinate(){ AMap.event.addListener(mapObj,'click',getLnglat); //点击事件 } function toolBarShow(){ toolBar.show(); toolBar.showRuler(); toolBar.showDirection(); } function toolBarDirection(){ toolBar.show(); toolBar.showDirection(); toolBar.hideRuler(); } function toolBarLong(){ toolBar.show(); toolBar.hideDirection(); toolBar.showRuler(); } function toolBarShot(){ toolBar.show(); toolBar.hideRuler(); toolBar.hideDirection(); } function iMapType(){ mapObj.plugin(["AMap.MapType"],function(){ //添加舆图范例切换插件 //舆图范例切换 mapType= new AMap.MapType({defaultType:1,showRoad:true}); mapObj.addControl(mapType); }); } function removeMapType(){ mapObj.removeControl(mapType); } function iCircleEditor(){ //圆形编辑器 circle = new AMap.Circle({ //圆形编辑器的样式 map: mapObj, center:new AMap.LngLat("116.40332221984863","39.90025505675715"), radius:1000, strokeColor: "#F33", strokeOpacity: 1, strokeWeight: 3, fillColor: "ee2200", fillOpacity: 0.35 }); mapObj.plugin(["AMap.CircleEditor"],function(){ circleEditor = new AMap.CircleEditor(mapObj,circle); //建设圆形编辑器工具 circleEditor.open(); //打开圆形编辑器 }); } function removeCicleEditor(){ //封锁圆形编辑器,埋没圆形 circleEditor.close(); circle.hide(); } function iPloygonEditor(){ //编辑多边形 var arr=new Array();//经纬度坐标数组 arr.push(new AMap.LngLat("116.403322","39.920255")); arr.push(new AMap.LngLat("116.410703","39.897555")); arr.push(new AMap.LngLat("116.402292","39.892353")); arr.push(new AMap.LngLat("116.389846","39.891365")); polygon = new AMap.Polygon({ path:arr, //配置多边形表面的节点数组 strokeColor:"#0000ff", strokeOpacity:0.2, strokeWeight:3, fillColor: "#f5deb3", fillOpacity: 0.35 }); //舆图上添加多边形 mapObj.addOverlays(polygon); //结构多边形编辑工具,并开启多边形的编辑状态 mapObj.plugin(["AMap.PolyEditor"],function(){ polygonEditor = new AMap.PolyEditor(mapObj,polygon); polygonEditor.open(); }); } function removePloygonEditor(){ polygonEditor.close(); polygon.hide(); } AMap.homeControlDiv = function(){} AMap.homeControlDiv.prototype = { addTo: function(map, dom){ dom.appendChild(this._getHtmlDom(map)); }, _getHtmlDom:function(map){ this.map=map; // 建设一个能承载控件的<div>容器 controlUI = document.createElement("DIV"); controlUI.style.width='80px'; //配置控件容器的宽度 controlUI.style.height='20px'; //配置控件容器的高度 controlUI.style.backgroundColor='white'; controlUI.style.borderStyle='solid'; controlUI.style.borderWidth='2px'; controlUI.style.cursor='pointer'; controlUI.style.textAlign='center'; // 配置控件的位置 controlUI.style.position='absolute'; controlUI.style.left='120px'; //配置控件离舆图的左界线的偏移量 controlUI.style.top='5px'; //配置控件离舆图上界线的偏移量 controlUI.style.zIndex='300'; //配置控件在舆图上显示 // 配置控件字体样式 controlUI.style.fontFamily='Arial,sens-serif'; controlUI.style.fontSize='12px'; controlUI.style.paddingLeft='4px'; controlUI.style.paddingRight='4px'; controlUI.innerHTML="换中心点"; // 配置控件响应点击onclick事件 controlUI.onclick = function(){ map.setCenter(new AMap.LngLat(116.234404, 39.12915)); } return controlUI; } } AMap.event.trigger(homeControlDiv,"hide"); AMap.event.addListener(homeControlDiv,"hide",function(){ controlUI.style.display = 'none'; }) function myControl(){ homeControl = new AMap.homeControlDiv(mapObj); //新建自界说插件工具 mapObj.addControl(homeControl); //舆图上添加插件 } function removeMyControl(){ homeControl.hide(); //controlUI.style.display='none'; } function iRangingTool(){ ruler.turnOn(); } function removeRangingTool(){ ruler.turnOff(); mapObj.clearMap(); //ruler.hide(); //ruler.setMap(null); //mapObj.removeControl(ruler); } function iMarker(){ mousetool.marker(); //利用鼠标东西,在舆图上画标志点 } function iMeasureArea(){ mousetool.measureArea(); } function iRectZoomIn(){ mousetool.rectZoomIn(); } function iRectZoomOut(){ mousetool.rectZoomOut(); } function iPolyline(){ mousetool.polyline(); } function iPolygon(){ mousetool.polygon(); } function iCircle(){ mousetool.circle(); } function iRectangle(){ mousetool.rectangle(); } function iRule(){ mousetool.rule(); } function removeMouseTool(){ mousetool.close(true); } function geocoder() { var MGeocoder; //加载地理编码插件 mapObj.plugin(["AMap.Geocoder"], function() { MGeocoder = new AMap.Geocoder({ radius: 1000, extensions: "all" }); //返回地理编码功效 AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack); //逆地理编码 MGeocoder.getAddress(lnglatXY); }); //加点 var marker = new AMap.Marker({ map:mapObj, icon: new AMap.Icon({ image: "http://api.amap.com/Public/images/js/mark.png", size:new AMap.Size(58,30), imageOffset: new AMap.Pixel(-32, -0) }), position: lnglatXY, offset: new AMap.Pixel(-5,-30) }); // mapObj.setFitView(); } //回调函数 function geocoder_CallBack(data) { var address; //返回地点描写 address = data.regeocode.formattedAddress; //返回功效拼接输出 document.getElementById("iAddress").innerHTML = address; } //鼠标点击,获取经纬度坐标 function getLnglat(e){ mapObj.clearMap(); var x = e.lnglat.getLng(); var y = e.lnglat.getLat(); document.getElementById("lnglat").innerHTML = x + "," + y; lnglatXY = new AMap.LngLat(x,y); geocoder(); } </script>
下面是HTML代码: