<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Hello, World</title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#container{height:92%}
#infoContainer{height:8%;background-color:#bbb}
.tableStyle{margin-left:20px;margin-right:20px;}
#td_ZoomNum{width:180px}
#td_centerPos{width:250px}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=87aa94f080dbd728ae7b32496ffc55ae">
//v2.0版本的引用方式:src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"
//v1.4版本及以前版本的引用方式:src="http://api.map.baidu.com/api?v=1.4&key=您的密钥&callback=initialize"
</script>
</head>
<body>
<div id="container"></div>
<div id="infoContainer">
<table class="tableStyle">
<tr>
<td id="td_ZoomNum">缩放等级 15</td>
<td id="td_centerPos">中心 (116.404, 39.915)</td>
<td id="td_pixelPos">像素坐标 (, )</td>
</tr>
<tr>
<td></td>
<td id="td_titlePos">图块坐标 (, )</td>
<td id="td_visiblePos">可视化坐标 (, )</td>
</tr>
</table>
</div>
<script type="text/javascript">
var map = new BMap.Map("container",{defaultCursor: 'default'}); // 创建地图实例
var point = new BMap.Point(116.404, 39.915); // 创建点坐标
map.centerAndZoom(point, 15); // 初始化地图,设置中心点坐标和地图级别
var TILE_SIZE =256;map.enableScrollWheelZoom(); //使能鼠标滚动缩放功能
//map.centerAndZoom("青岛", 12); // 初始化地图,设置中心点坐标和地图级别
//跳转到指定城市
function goToCity(city){
map.centerAndZoom(city, 11);
var center = map.getCenter();
getPosInfo(center);
}
//goCity(map, "青岛");
//跳转到指定经纬度
function goTolnglat(lnglat){
map.centerAndZoom(lnglat, 16);
var center = map.getCenter();
var marker = new BMap.Marker(lnglat);
map.addOverlay(marker);
getPosInfo(center);
}
goTolnglat(new BMap.Point(116.404, 39.915));
function getPosInfo(pos){
//平面坐标
var projection =map.getMapType().getProjection();
var worldCoordinate = projection.lngLatToPoint(pos);
//像素坐标
var pixelCoordinate =new BMap.Pixel(Math.floor(worldCoordinate.x * Math.pow(2, map.getZoom() -18)),
Math.floor(worldCoordinate.y * Math.pow(2, map.getZoom() -18)));
//图块坐标
var tileCoordinate =new BMap.Pixel(Math.floor(pixelCoordinate.x /256),
Math.floor(pixelCoordinate.y /256));
//可视化坐标
var viewportCoordinate = map.pointToPixel(pos);
var el = document.getElementById("td_centerPos");
el.innerHTML = "中心 (" + pos.lng + ", " + pos.lat+")";
var el = document.getElementById("td_pixelPos");
el.innerHTML = " 像素坐标 (" + pixelCoordinate.x + ", " + pixelCoordinate.y+")";
var el = document.getElementById("td_visiblePos");
el.innerHTML = "可视化坐标 (" + viewportCoordinate.x + ", " + viewportCoordinate.y+")";
var el = document.getElementById("td_titlePos");
el.innerHTML = "图块坐标 (" + tileCoordinate.x + ", " + tileCoordinate.y+")";
}
map.addEventListener('click', function(e){
var info =new BMap.InfoWindow('', {width: 260});
var projection =this.getMapType().getProjection();
var lngLat = e.point;
var lngLatStr ="经纬度:"+ lngLat.lng +", "+ lngLat.lat;
var worldCoordinate = projection.lngLatToPoint(lngLat);
var worldCoordStr ="<br />平面坐标:"+ worldCoordinate.x +", "+ worldCoordinate.y;
var pixelCoordinate =new BMap.Pixel(Math.floor(worldCoordinate.x * Math.pow(2, this.getZoom() -18)),
Math.floor(worldCoordinate.y * Math.pow(2, this.getZoom() -18)));
var pixelCoordStr ="<br />像素坐标:"+ pixelCoordinate.x +", "+ pixelCoordinate.y;
var tileCoordinate =new BMap.Pixel(Math.floor(pixelCoordinate.x /256),
Math.floor(pixelCoordinate.y /256));
var tileCoordStr ="<br />图块坐标:"+ tileCoordinate.x +", "+ tileCoordinate.y;
var viewportCoordinate = map.pointToPixel(lngLat);
var viewportCoordStr ="<br />可视区域坐标:"+ viewportCoordinate.x +", "+ viewportCoordinate.y;
info.setContent(lngLatStr + worldCoordStr + pixelCoordStr + tileCoordStr +
viewportCoordStr);
map.openInfoWindow(info, lngLat);
});
map.addEventListener("dragend", function(){
//中心点坐标
var center = map.getCenter();
getPosInfo(center);
});
map.addEventListener("zoomend", function(){
//地图缩放级别
var el = document.getElementById("td_ZoomNum");
el.innerHTML = "缩放等级 " + this.getZoom();
//中心点坐标
var center = map.getCenter();
getPosInfo(center);
});
var center = map.getCenter();
getPosInfo(center);
</script>
</body>
</html>
- 1
- 2
- 3
前往页