Filippo oretti
Filippo oretti

Reputation: 49817

OpenLayer map markers don't works

hi i'm using Openlayer map this is my script , map is ok but the markers wouldn't works:

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

this is the console log:

enter image description here when i do not use markers the map zoom is exactly in the lon and lat i pass, while if i use marker the marker is putted really outside of the place they needs to be on.

does anyone has suggestions?

Upvotes: 1

Views: 1143

Answers (1)

user1702401
user1702401

Reputation: 1658

Currently you are:

  1. creating markers from original coordinates
  2. transforming coordinates from 4326 to 900913 and using them to center map.

In what system are your coordinates in _json object? In case, they are EPSG:4325 (from -180 to 180 or 0 to 360), create markers from transformed coordinates:

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, is there any particular reason to create two markers to same place:

markers.addMarker(new OpenLayers.Marker(lonLat, icon.clone()));

Upvotes: 3

Related Questions