You are not logged in.

Announcement

*** NOTICE: forum.openstreetmap.org is being retired. Please request a category for your community in the new ones as soon as possible using this process, which will allow you to propose your community moderators.
Please create new topics on the new site at community.openstreetmap.org. We expect the migration of data will take a few weeks, you can follow its progress here.***

#1 2011-11-11 17:31:13

support98
Member
Registered: 2011-03-21
Posts: 11

Coordinates via MouseClick

I have embed a simply osm map in a website with this code:

function init() {
        map = new OpenLayers.Map("basicMap");
        var mapnik = new OpenLayers.Layer.OSM();
        map.addLayer(mapnik);
        map.setCenter(new OpenLayers.LonLat(13.41,52.52) // Center of the map
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
          ), 15 // Zoom level
        );
      }

...

<body onload="init();">
    <div id="basicMap"></div>
    <body>

Now I want that when you click on the map you should get the coordinates of this point. I have found this code:

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {               
        defaultHandlerOptions: {
            'single': true,
            'double': false,
            'pixelTolerance': 0,
            'stopSingle': false,
            'stopDouble': false
        },
   
        initialize: function(options) {
            this.handlerOptions = OpenLayers.Util.extend(
                {}, this.defaultHandlerOptions
            );
            OpenLayers.Control.prototype.initialize.apply(
                this, arguments
            );
            this.handler = new OpenLayers.Handler.Click(
                this, {
                    'click': this.trigger
                }, this.handlerOptions
            );
        },
   
        trigger: function(e) {
            position = this.map.getLonLatFromViewPortPx(e.xy);
alert("x="+position.x+", y="+position.y);

}


But when I enter this code, I see only a white screen. What is wrong?
Where do I have to enter this code? And I have heard that I have to change some variables. Which and how do I have to name them?

Thanks.

Offline

Board footer

Powered by FluxBB