1. 创建地图,加载控件
var map = new OpenLayers.Map("map", {
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
map.addControl(new OpenLayers.Control.Permalink());
map.addControl(new OpenLayers.Control.MousePosition());
2. 创建世界范围的基于墨卡托投影的地图
var options = {
projection: new OpenLayers.Projection("EPSG:900913"),//墨卡托投影
units: "m", //以米为单位
maxResolution: 156543.0339, //最大分辨率,0级 图上一个像素表示实际距离15万米多
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34,20037508.34, 20037508.34)
//将地球分为4个卦象,156543.0339*128=20037508.34(为什么是*128,一个256*256的jpg中分,-128~128)
}; map = new OpenLayers.Map('map', options);
3. 坐标转换后居中
var proj = new OpenLayers.Projection("EPSG:4326");
var point = new OpenLayers.LonLat(119,40);
point.transform(proj, map.getProjectionObject());
map.setCenter(point);//地理坐标
4. bounds 转换后居中
var bounds = new OpenLayers.Bounds(-74.047185, 40.679648, -73.907005, 40.882078);
bounds.transform(proj, map.getProjectionObject());
5.
4.