1.不能够正常添加标记到地图上,当使用GOOGLE地图时
原因: 添加标记的时候需要加上中扩号-> map.addLayers([markers]);
var lon = 107.5781;
var lat = 36.8796;
var zoom = 2;
var map, layer,gmap,markers;
var objMouse;
var options = {
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326"),
units: "m",
maxResolution: 156543.0339,
numZoomLevels: 10,
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34,
20037508.34, 20037508.34)
};
map = new OpenLayers.Map('map',options);
createPopup();
function createPopup()
{
markers = new OpenLayers.Layer.Markers( "Markers" );
markers.addMarker(GetMark(lon,lat));
markers.events.register('click', markers, function(){
popup = new OpenLayers.Popup("chicken",
new OpenLayers.LonLat(lon,lat),
new OpenLayers.Size(20,20),
"haha!!!",
true);
popup.autoSize=true;
map.addPopup(popup);
});
map.addLayers([markers]);
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
}
function GetMark(lo,lat)
{
var size = new OpenLayers.Size(70,30);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://www.k1982.com/png/up/200905/20090513082309637.png',size,offset);
return new OpenLayers.Marker(new OpenLayers.LonLat(lon,lat),icon)
}
2.用Google的地图,添加标记时候用的经纬不能准确定位标记
像素值与实际经纬度值的互转
map.events.register('click',gmap,function(ev){
var lonLat=map.getLonLatFromPixel(ev.xy);
if (map.displayProjection) {
//将像素值转换成经纬度
lonLat.transform(map.getProjectionObject(),map.displayProjection );
//将经纬度转换成像素值
lonLat.transform(map.displayProjection,map.getProjectionObject());
}
3.用经纬度来为地图添加标记
var laolat_v=new OpenLayers.LonLat(lo,la);
if (map.displayProjection) {
laolat_v.transform(map.displayProjection,map.getProjectionObject());
}
markers = new OpenLayers.Layer.Markers( "Markers" );
markers.addMarker(GetMark(laolat_v));
markers.events.register('click', markers, function(){
popup = new OpenLayers.Popup("chicken",
laolat_v,
new OpenLayers.Size(20,20),
"haha!!!",
true);
popup.autoSize=true;
map.addPopup(popup);
});
map.addLayers([markers]);
}
function GetMark(laolat_v)
{
var size = new OpenLayers.Size(70,30);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://www.k1982.com/png/up/200905/20090513082309637.png',size,offset);
var mk=new OpenLayers.Marker(laolat_v,icon)
//mk.visible=false;
return mk;
}
4、以对图片载入错误时图片框的颜色和图片框中默认的图片进行自定义
由OpenLayers.Util类控制,具体修改openlayers/theme/default/style.css 中
.olImageLoadError {
background-color: pink;
opacity: 0.5;
filter: alpha(opacity=0); /* IE */
}