主頁(yè) > 知識(shí)庫(kù) > 高德地圖WEB版基礎(chǔ)控件展示 原創(chuàng)

高德地圖WEB版基礎(chǔ)控件展示 原創(chuàng)

熱門(mén)標(biāo)簽:檢查注冊(cè)表項(xiàng) 銀行業(yè)務(wù) 美圖手機(jī) 網(wǎng)站文章發(fā)布 智能手機(jī) 鐵路電話(huà)系統(tǒng) 服務(wù)器配置 呼叫中心市場(chǎng)需求

之前想自己做一個(gè)旅游導(dǎo)航的項(xiàng)目,在網(wǎng)上一搜發(fā)現(xiàn)了高德地圖開(kāi)放平臺(tái),發(fā)現(xiàn)原來(lái)高德可以很簡(jiǎn)單的就應(yīng)用到自己的項(xiàng)目里面,當(dāng)即我就申請(qǐng)了一個(gè)key來(lái)學(xué)一學(xué),仔細(xì)研究了一下,感覺(jué)還挺難的,網(wǎng)上找了找案例什么的,經(jīng)過(guò)這幾天,小編把高德的一些基礎(chǔ)控件差不多弄了一下,效果圖如下圖所示:

廢話(huà)不多說(shuō),直接上源碼,下面是js代碼:

script language="javascript" src="http://webapi.amap.com/maps?v=1.2key=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;
//刷新頁(yè)面
function reload(){
	location.reload();
}

function mapInit(){
 mapObj = new AMap.Map("iCenter",{
		center:new AMap.LngLat(116.397728,39.90423), //地圖中心點(diǎn)
		level:13, //地圖顯示的比例尺級(jí)別
	});
	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(); //展開(kāi)鷹眼
 });
	mapObj.plugin(["AMap.RangingTool"],function(){ 
		ruler = new AMap.RangingTool(mapObj); 
		AMap.event.addListener(ruler,"end",function(e){ 
 ruler.turnOff(); 
 }); 		
	}); 
	mapObj.plugin(["AMap.MouseTool"],function(){ 	 //鼠標(biāo)工具插件
		mousetool = new AMap.MouseTool(mapObj); 		 
	});
}
function Coordinate(){
	AMap.event.addListener(mapObj,'click',getLnglat); //點(diǎn)擊事件
}


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(){ //添加地圖類(lèi)型切換插件
 //地圖類(lèi)型切換
 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); //創(chuàng)建圓形編輯器對(duì)象
		circleEditor.open(); //打開(kāi)圓形編輯器
	});
}
function removeCicleEditor(){ //關(guān)閉圓形編輯器,隱藏圓形
	circleEditor.close();
	circle.hide();
}

function iPloygonEditor(){ //編輯多邊形
 var arr=new Array();//經(jīng)緯度坐標(biāo)數(shù)組
 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, //設(shè)置多邊形輪廓的節(jié)點(diǎn)數(shù)組
 strokeColor:"#0000ff",
 strokeOpacity:0.2,
 strokeWeight:3,
 fillColor: "#f5deb3",
 fillOpacity: 0.35
 });
 //地圖上添加多邊形
 mapObj.addOverlays(polygon);
 //構(gòu)造多邊形編輯對(duì)象,并開(kāi)啟多邊形的編輯狀態(tài)
 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;
 // 創(chuàng)建一個(gè)能承載控件的div>容器
 controlUI = document.createElement("DIV");
 controlUI.style.width='80px'; //設(shè)置控件容器的寬度
 controlUI.style.height='20px'; //設(shè)置控件容器的高度
 controlUI.style.backgroundColor='white';
 controlUI.style.borderStyle='solid';
 controlUI.style.borderWidth='2px';
 controlUI.style.cursor='pointer';
 controlUI.style.textAlign='center';

 // 設(shè)置控件的位置
 controlUI.style.position='absolute';
 controlUI.style.left='120px'; //設(shè)置控件離地圖的左邊界的偏移量
 controlUI.style.top='5px'; //設(shè)置控件離地圖上邊界的偏移量
 controlUI.style.zIndex='300'; //設(shè)置控件在地圖上顯示

 // 設(shè)置控件字體樣式
 controlUI.style.fontFamily='Arial,sens-serif';
 controlUI.style.fontSize='12px';
 controlUI.style.paddingLeft='4px';
 controlUI.style.paddingRight='4px';
 controlUI.innerHTML="換中心點(diǎn)";

 // 設(shè)置控件響應(yīng)點(diǎn)擊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); //新建自定義插件對(duì)象
	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(); //使用鼠標(biāo)工具,在地圖上畫(huà)標(biāo)記點(diǎn)
}
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"
 });
 //返回地理編碼結(jié)果 
 AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack); 
 //逆地理編碼
 MGeocoder.getAddress(lnglatXY); 
 });
 //加點(diǎn)
 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();
}
//回調(diào)函數(shù)
function geocoder_CallBack(data) {
 var address;
 //返回地址描述
 address = data.regeocode.formattedAddress;
 //返回結(jié)果拼接輸出
 document.getElementById("iAddress").innerHTML = address;
} 
//鼠標(biāo)點(diǎn)擊,獲取經(jīng)緯度坐標(biāo) 
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代碼:

body onLoad="mapInit()">
 div id="iCenter">/div>
 div id="iControlbox">
		ul>
			li>button onclick="javascript:toolBarShow();">顯示完整魚(yú)骨/button>button onclick="javascript:toolBar.hide();">隱藏魚(yú)骨/button>button onclick="javascript:toolBarDirection();">方向盤(pán)/button>button onclick="javascript:toolBarLong();">長(zhǎng)標(biāo)尺/button>button onclick="javascript:toolBarShot();">短標(biāo)尺/button>/li>
			li>button onclick="javascript:scale.show();">顯示比例尺/button>button onclick="javascript:scale.hide();">隱藏比例尺/button>/li>
			li>button onclick="javascript:overView.show();">顯示鷹眼/button>button onclick="javascript:overView.hide();">隱藏鷹眼/button>/li>
			li>button onclick="javascript:iMapType();">添加地圖類(lèi)型切換/button>button onclick="javascript:removeMapType();">移除地圖類(lèi)型切換/button>/li>
			li>button onclick="javascript:iCircleEditor();">添加圓形編輯器/button>button onclick="javascript:removeCicleEditor();">刪除圓形編輯器/button>/li>
			li>button onclick="javascript:iPloygonEditor();">添加多邊形編輯器/button>button onclick="javascript:removePloygonEditor();">刪除多邊形編輯器/button>/li>			
			li>button onclick="javascript:iMarker();">鼠標(biāo)打點(diǎn)工具/button>button onclick="javascript:removeMouseTool();">清除/button>/li>		
			li>button onclick="javascript:iPolyline();">鼠標(biāo)畫(huà)折線(xiàn)工具/button>button onclick="javascript:removeMouseTool();">清除/button>/li>	
			li>button onclick="javascript:iPolygon();">鼠標(biāo)畫(huà)多邊形工具/button>button onclick="javascript:removeMouseTool();">清除/button>/li>
			li>button onclick="javascript:iCircle();">鼠標(biāo)畫(huà)圓形工具/button>button onclick="javascript:removeMouseTool();">清除/button>/li>
			li>button onclick="javascript:iRectangle();">鼠標(biāo)畫(huà)矩形工具/button>button onclick="javascript:removeMouseTool();">清除/button>/li>
			li>button onclick="javascript:iRule();">鼠標(biāo)測(cè)距工具/button>button onclick="javascript:removeMouseTool();">清除/button>/li>
			li>button onclick="javascript:iMeasureArea();">鼠標(biāo)測(cè)面積/button>button onclick="javascript:removeMouseTool();">移除/button>/li>
			li>button onclick="javascript:iRectZoomIn();">鼠標(biāo)框選縮小/button>button onclick="javascript:iRectZoomOut();">鼠標(biāo)框選放大/button>button onclick="javascript:removeRangingTool();">關(guān)閉鼠標(biāo)放大縮小/button>/li>
			li>button onclick="javascript:iRangingTool();">測(cè)距插件/button>button onclick="javascript:removeRangingTool();">隱藏測(cè)距/button>/li>
			li>button onclick="javascript:myControl();">添加自定義控件/button>button onclick="javascript:removeMyControl();">移除自定義控件/button>/li>
			li>div class="detail">p>span id="lnglat">nbsp;/span>/p>p>span id="iAddress">nbsp;/span>/p>/div>/li>
			li>button onclick="javascript:Coordinate();">坐標(biāo)拾取控件/button>button onclick="javascript:reload();">取消坐標(biāo)拾取/button>/li>

			
		/ul>
	/div>
/body>

在js第一行引入高德地圖API中,key值是我自己在高德地圖里面創(chuàng)建的也可以用,你們也可以上高德開(kāi)放平臺(tái)自行申請(qǐng)key值試一試。

高德開(kāi)放平臺(tái):developer.amap.com/

這里是本效果圖源碼下載地址,感興趣的朋友可以直接下載

點(diǎn)擊此處下載

標(biāo)簽:新疆 上海 長(zhǎng)治 滄州 紅河 河南 沈陽(yáng) 樂(lè)山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《高德地圖WEB版基礎(chǔ)控件展示 原創(chuàng)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266