I have a new marker to place on the map. I use the following code
var markers = [{'ID':'431','LATITUDE':-33.85491,'LONGITUDE':151.19877}];
var map, layer;
function loadMarkers(POIs){
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var size = new OpenLayers.Size(10,17);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
for (var i=0;i<POIs.length;i++){
var MarkerDef = POIs[i];
var icon = new OpenLayers.Icon(
'http://openlayers.org/dev/img/marker-gold.png',
size,offset);
var LonLat = new OpenLayers.LonLat(
MarkerDef.LONGITUDE,MarkerDef.LATITUDE);
var marker = new OpenLayers.Marker(LonLat,icon);
markers.addMarker(marker);
}
map.addControl(new OpenLayers.Control.Permalink());
if (!map.getCenter()) map.zoomToMaxExtent();
}
function init(){
OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
map = new OpenLayers.Map( $('map'),
{'maxResolu开发者_高级运维tion': 360/512});
var tiles_url = "http://xxx.xx.xxx.xx:8000/";
var tilelite_layer = new OpenLayers.Layer.OSM(
"Mapnik",
tiles_url + '${z}/${x}/${y}.png');
tilelite_layer.attribution =
"Data served by "+
"<a href='http://bitbucket.org/springmeyer/tilelite/'>"+
"TileLite</a>";
var osm_official_tiles = new OpenLayers.Layer.OSM(
"OpenStreetMap Mapnik Server (Mod_tile)");
osm_official_tiles.attribution =
"Map Data CC-BY-SA Openstreetmap.org";
map.addLayers([tilelite_layer,osm_official_tiles]);
loadMarkers(markers);
}
The map is drawn correctly and everything is ok, except that the marker does not appear at -33.85491,151.19877. It appears in the middle of the map. Any suggestions are welcome.
Fixed the problem, I needed to change the projection:
var LonLat = new OpenLayers.LonLat( MarkerDef.LONGITUDE,MarkerDef.LATITUDE )
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
精彩评论