嗨我正在使用Openlayer地图这是我的脚本,地图还可以,但标记不起作用:
var map = new OpenLayers.Map ('open_map', {
controls:[
new OpenLayers.Control.Navigation()
],
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
var lonLat = new OpenLayers.LonLat(_json.lon, _json.lat).transform (
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var size = new OpenLayers.Size(16,27);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://www.google.com/mapfiles/marker.png', size, offset);
markers.addMarker(new OpenLayers.Marker(lonLat, icon.clone()));
var mapnik = new OpenLayers.Layer.OSM("Test");
map.addLayer(mapnik);
map.setCenter (lonLat, 5 /* zoom lvl */);
console.log(lonLat);
console.log(_json.lon);
console.log(_json.lat);
这是控制台日志:
当我不使用标记时,地图缩放正好在lon和lat i中,而如果我使用标记,标记就会被放置在他们需要的地方之外。
有人有建议吗?
答案 0 :(得分:3)
目前你是:
你在_json对象中的坐标是什么系统?如果它们是EPSG:4325(从-180到180或0到360),则从变换坐标创建标记:
var lonLat = new OpenLayers.LonLat(_json.lon, _json.lat).transform (
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
// map.getProjectionObject() doesn't work for unknown reason
);
markers.addMarker(new OpenLayers.Marker(lonLat, icon));
markers.addMarker(new OpenLayers.Marker(lonLat, icon.clone()));
map.setCenter (lonLat, 5 /* zoom lvl */);
PS,有没有特别的理由在同一个地方创建两个标记:
markers.addMarker(new OpenLayers.Marker(lonLat, icon.clone()));