Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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());
                this.MapCanvas.zoomToMaxExtent();

                //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);
                    alert(this.position);
                });
            }
        };

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

share|improve this question
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.