困扰我的关于OpenLayers的问题终于解决了:

原因: 添加标记的时候需要加上中扩号-> 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;
}
#Openlayers

 

你可能感兴趣的:(OpenLayers,openlayers)