Openlayers标记出现在地图的中心

时间:2011-02-03 04:07:55

标签: gis openlayers

我有一个新标记要放在地图上。我使用以下代码

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'), 
      {'maxResolution': 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);
}

正确绘制地图并且一切正常,但标记未出现在-33.85491,151.19877。它出现在地图的中间。 欢迎任何建议。

1 个答案:

答案 0 :(得分:6)

修正了问题,我需要更改投影:

 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
      );