0

I am unable to put marker on specific lat,long here what is i am trying..

<script src="http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.11/OpenLayers.js"></script>

<script>
  function init() {
    map = new OpenLayers.Map("basicMap");
    var mapnik         = new OpenLayers.Layer.OSM();
    var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
    var toProjection   = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
    var position       = new OpenLayers.LonLat(77.5656523,22.545454).transform( fromProjection, toProjection);
    var zoom           = 3; 

    map.addLayer(mapnik);
    map.setCenter(position, zoom ); 

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);

    var lonLat = new OpenLayers.LonLat( 77.1279688 ,51.5043436 );        

        var size = new OpenLayers.Size(30,30);
        var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
        var icon = new OpenLayers.Icon('../../assets_icon/sm_icon/lorry1.png', size, offset);
    var marker = new OpenLayers.Marker(lonLat,icon);
        markers.addMarker(marker);

     marker.events.register("click", marker, function(e){
     popup = new OpenLayers.Popup.FramedCloud("",
                           marker.lonlat,
                           new OpenLayers.Size(200, 200),
                           "here lies your information",
                           null, true);

      map.addPopup(popup);
    }); 

      }
</script>

Edit test image

what is here i am missing?

  • What happens? Is there an error message? – til_b May 11 '16 at 12:22
  • @til_b there is no error message but my marker is stick to gulf of guinea whatever lat,lng i put. – Abhishek Gurjar May 11 '16 at 12:24
0

You need to transform your marker position to EPSG:900913 also.

var lonLat = new OpenLayers.LonLat( 77.1279688 ,51.5043436 ).transform( fromProjection, toProjection); 

Coordinates are given in different coordinate reference systems. Those reference systems are accessible through EPSG codes, where EPSG is the "European Petroleum Survey Group Geodesy". There is an online database of EPSG codes, at http://spatialreference.org/ref/epsg/ .

WGS84 (which is EPSG:4326) expects coordinates in decimal degrees, and has the null meridian at Greenwich. The Web mercator projection system used by Google, OpenStreetMap and others is accessible through EPSG:900913 or EPSG:3857 (both are the same, 900913 is an inofficial value chosen by google, because the numbers somewhat represent the string "google").

So while WGS84 coordinates in decimal degrees range is from -180/-90 to 180/90, web mercator coordinates range is -20026376.39/-20048966.10 to 20026376.39/20048966.10.

For more information see wikipedia on coordinate systems, https://en.wikipedia.org/wiki/Geographic_coordinate_system and https://en.wikipedia.org/wiki/Map_projection , but keep in mind that this is a whole field of study which can get complex very soon. So don't despair if you don't understand it all.

  • I am new to openlayers can you please explain what is EPSG:900913 and what are these transformations. – Abhishek Gurjar May 12 '16 at 5:24
  • Do you know where did i find tutorial about openlayers and related stuff. – Abhishek Gurjar May 12 '16 at 11:43
  • Yes, of course. Just google "openlayers tutorial", theres plenty of them. For example docs.openlayers.org/library/introduction.html . But if you just start a new mapping project, I'd use leaflet (just google it). Its easier for beginners. – til_b May 12 '16 at 11:59
  • thank you, for your time i have one more question you can check my profile if you can answer it, it is about markerwithlabel in openlayers. – Abhishek Gurjar May 12 '16 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.