1. 程式人生 > >百度地圖API 重新生成點聚合的功能

百度地圖API 重新生成點聚合的功能

百度點聚合用來解決載入大量點要素到地圖上產生覆蓋現象的問題,並提高效能。

http://api.map.baidu.com/library/MarkerClusterer/1.2/docs/symbols/BMapLib.MarkerClusterer.html

最基本的實現(在此兩個按鈕的功能沒有實現):

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html {width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}
		#allmap{width:100%;height:80%;}
		#r-result{width:100%;}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的金鑰"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
	<title>點聚合</title>
</head>
<body>
	<div id="allmap"></div>
	<div id="r-result">
		<input type="button" onclick="add_overlay();" value="新增點聚合" />
		<input type="button" onclick="remove_overlay();" value="重新生成點聚合" />
	</div>
</body>
</html>
<script type="text/javascript">
	// 百度地圖API功能
	var map = new BMap.Map("allmap");
	map.centerAndZoom(new BMap.Point(116.404, 39.915), 4);
	map.enableScrollWheelZoom();

	var MAX = 10;
	var markers = [];
	var markerClusterer = null;
	for (var i = 0; i < MAX; i++) {
		var pt = new BMap.Point(Math.random()*40+85, Math.random()*30+21);
		var marker = new BMap.Marker(pt);
		map.addOverlay(marker);
		markers.push(marker);
	}
  
	//最簡單的用法,生成一個marker陣列,然後呼叫markerClusterer類即可。
	markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});  
</script>

實現兩個按鈕的功能,用於新增點聚合,和刪除點聚合並重新載入新的點
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html {width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}
		#allmap{width:100%;height:80%;}
		#r-result{width:100%;}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的金鑰"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
	<title>點聚合</title>
</head>
<body>
	<div id="allmap"></div>
	<div id="r-result">
		<input type="button" onclick="add_overlay();" value="新增點聚合" />
		<input type="button" onclick="remove_overlay();" value="重新生成點聚合" />
	</div>
</body>
</html>
<script type="text/javascript">
	// 百度地圖API功能
	var map = new BMap.Map("allmap");
	map.centerAndZoom(new BMap.Point(116.404, 39.915), 4);
	map.enableScrollWheelZoom();

	var MAX = 10;
	var markers = [];
	var markerClusterer = null;
	for (var i = 0; i < MAX; i++) {
		var pt = new BMap.Point(Math.random()*40+85, Math.random()*30+21);
		var marker = new BMap.Marker(pt);
		map.addOverlay(marker);
		markers.push(marker);
	}
  
	function add_overlay(){
		//最簡單的用法,生成一個marker陣列,然後呼叫markerClusterer類即可。
		markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
	}

	function remove_overlay(){
		for(var i=0; i<4; i++){
			map.removeOverlay(markers[i]);
		}
		var markers1 = markers.slice(4,markers.length);
		markerClusterer.clearMarkers();			//此步驟需要
		markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers1});
	}  
</script>