openlayers 应用Google图层你必须做的

//全局变量
var map,EPSG4326,EPSG900913,lineFeature_map,vectorLayer_map,markers_map;
function init(){
var lonLat= new OpenLayers.LonLat( 121.123, 38.9);
//很重要因为谷歌用得图层投影不一样,所以要统一图层 全要转成该种投影
var XY = lonLat.clone().transform( EPSG4326, EPSG900913 );

var options = {
controls: [],
maxResolution: “auto”,
projection: EPSG900913,
// displayProjection: EPSG4326,
munZoomLevels: 5,
units: “m”,
maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
20037508,20037508.34)
};
//加载地图
map= new OpenLayers.Map(‘trackInfoMap’, options);
var gphy = new OpenLayers.Layer.Google(
“Google Physical”,
{ type: G_PHYSICAL_MAP ,numZoomLevels: 20,’sphericalMercator’:true,}
);
var gmap = new OpenLayers.Layer.Google(
“Google Streets”, // the default
{numZoomLevels: 20 ,’sphericalMercator’:true,}
);
var ghyb = new OpenLayers.Layer.Google(
“Google Hybrid”,
{ type: G_HYBRID_MAP, numZoomLevels: 20 ,’sphericalMercator’:true,}
);
var gsat = new OpenLayers.Layer.Google(
“Google Satellite”,
{ type: G_SATELLITE_MAP, numZoomLevels: 20,’sphericalMercator’:true, }
);
vectorLayer_map = new OpenLayers.Layer.Vector(“Vector Layer”,
{ styleMap: new OpenLayers.StyleMap(featureStyle) });
markers_map = new OpenLayers.Layer.Markers(“marker”);
map.addLayer(vectorLayer_map);
map.addLayer(markers_map);
var style_green = {
strokeColor: “#339933”,
strokeOpacity: 1,
strokeWidth: 3,
pointRadius: 6,
pointerEvents: “visiblePainted”
};

lineFeature_map = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString(XY.lon, XY.lat), null, style_green);
vectorLayer_map.addFeatures([lineFeature_map]);
//添加图层
map.addLayers([gmap,gphy ,ghyb ,gsat ]);
//设置中心点坐标及放大倍数
map.setCenter(XY,9);
fn_Line();
//移除下边的logo 看着有使用说明啥的 确实不舒服
$(“div”).remove(“.olLayerGoogleCopyright”);
$(“div”).remove(“.olLayerGooglePoweredBy&gmnoprint”);
//其实还有很多东西没写 第一写博客 ,也是第一次用这个写,写着很不舒服 ,因为我也//不会弄.还有好多的 控制条啥的 要根据自己需要自己加吧,就写到这吧。opanlayers 确实很强大
}
//画图函数
function fn_Line() {
var px,py;
if (x < 20) {
px = Math.random() * (122 – 121 + 0.001) + 121;
py = Math.random() * (30 – 29 + 0.001) + 38;
var lonLat = new OpenLayers.LonLat(px, py);
var lonLat_ = lonLat.clone().transform(EPSG4326, EPSG900913);
var p1_ = new OpenLayers.Geometry.Point(lonLat_lon, lonLat_lat);
//划线
lineFeature_map.geometry.addPoint(p1_);
vectorLayer_map.drawFeature(lineFeature_map);
//加标记
markers_map.clearMarkers();
var marker = new OpenLayers.Marker(lonLat_);
markers_map.addMarker(marker);
x++;
setTimeout(fn, 1000);
}
}
转载自:https://blog.csdn.net/iteye_19276/article/details/82214559

You may also like...