I have created a map using openlayrs, and put a click event to display its position in screen, when i clicked got an error message like Uncaught TypeError: Cannot call method 'getLonLatFromPixel' of undefined , another error like Uncaught TypeError: Cannot call method 'xy_to_ll' of undefined., I've tried OpenLayers.Control.Click but it shows some other errors, like e is undefined.

And I've another problem with openlayers google map, on setting the mapCenter wms,yahoo maps shows the exact location, but google won't. How to fix this.I have figured out one solution for this to set sphericalMercator, but this doesn't fixes that problem

My code is given below:

//canvasId is the id of html element. 
Mapper =
            MapCanvas: null,
            position: null,
            MapperInit: function (canvasId) {
                /// <param name="canvasId" type="String">Id of the element on which the map is to be generated</param>

                this.MapCanvas = new OpenLayers.Map(canvasId, {
                    layers: [
                        new OpenLayers.Layer.Google("Google", { 'sphericalMercator': true,
                            'maxExtent': new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34)
                        new OpenLayers.Layer.WMS("WMS","http://vmap0.tiles.osgeo.org/wms/vmap0", { layers: "basic" }),
                        new OpenLayers.Layer.Yahoo("Yahoo")
                //Adding controls to map

                this.MapCanvas.addControl(new OpenLayers.Control.LayerSwitcher());
                this.MapCanvas.addControl(new OpenLayers.Control.MousePosition());
                this.MapCanvas.addControl(new OpenLayers.Control.OverviewMap());

                //sets the map center
                this.MapCanvas.setCenter(new OpenLayers.LonLat( 77.5, 8.5), 5);

                this.MapCanvas.events.register("click", this.MapCanvas, function (e) {
                    console.log("Value of E => "+e.xy);
                    this.position = this.MapCanvas.getLonLatFromPixel(e.xy);

Please help me to find what is wrong in my code.

  • I've found the problem , It is this.MapCanvas is not available inside the event fuction, can anyone help me – bios Aug 9 '12 at 6:13

Your Answer

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

Browse other questions tagged or ask your own question.