I use google chromes inspect element for debugging.
I have a map displayed on my page with 1 base layer (osm) and 2 overlays (wards, points) and a highlighter control. The map displays and works fine.
The problem is that in the inspector, the map object is undefined and so are any other objects defined in the javascript but they are working. I have also checked this in firebug, the result is same.
To elaborate this, I have a div ided "map1", and it holds map object named "map", the map has baselayer named "osm" and overlays named "wards" and "points". As shown in picture, map1 is recognized as HTML div element but map is undefined whereas it should have recognized it as Openlayers.Map object.
here is the code, i hope you don't need the files, any valid osm file should work. (it is not a problem with openlayers, but with browsers)
<html>
<head>
<title></title>
<link rel="stylesheet" href="http://openlayers.org/api/2.11/theme/default/style.css" type="text/css">
<!--link rel="stylesheet" href="../../../openlayers-2.12/theme/default/style.css" type="text/css"-->
<script src="http://openlayers.org/api/2.11/OpenLayers.js" type="text/javascript"></script>
<!--script src="../../../openlayers-2.12/lib/OpenLayers.js"></script-->
<!--script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=en-us"></script--> <!--This is required wrapper for the bing images-->
<!--script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script-->
<!--script src="../OpenStreetMap.js"></script-->
<script>
function init(){
//alert("Start here");
//OpenLayers.ProxyHost= "/cgi-bin/proxy.cgi?url=";
//map options
var centerX = 85.381540;//491213.721224323//-123.1684986291807;//9497800;
var centerY = 27.710237;//5456645.24607268//49.245339757767844;//3212000;
var center = new OpenLayers.LonLat(centerX, centerY);
var range = 0.0025;//10000000//.5;//10000;
var map_bound = [centerX-range,centerY-range,centerX+range,centerY+range];
var extent = new OpenLayers.Bounds(map_bound[0],map_bound[1],map_bound[2],map_bound[3]);
var zoom = 13;
var zoom_data_limit = 18; // vector data will load only in this level or above
//other options
var proj4326 = new OpenLayers.Projection("EPSG:4326");
var proj900913 = new OpenLayers.Projection("EPSG:900913");
var popup;
//map configuration
var map = new OpenLayers.Map('map1',{
//allOverlays:true,
maxExtent:extent,
controls:[new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.MousePosition({
suffix:'',
emptyString:'',
displayProjection:new OpenLayers.Projection("EPSG:4015")
}),
//new OpenLayers.Control.ScaleLine(),
//new OpenLayers.Control.Scale(),
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Attribution()
],
projection:proj4326,
displayProjection:proj900913
});
bing = new OpenLayers.Layer.Bing({name: "Bing Aerial Layer",type: "Aerial",key: "AqTGBsziZHIJYYxgivLBf0hVdrAk9mWO5cQcb8Yux8sW5M8c8opEC2lZqKR1ZZXf"});
osm = new OpenLayers.Layer.OSM("OSM");
map.addLayer(osm);
map.addLayer(bing);
map.setCenter(center.transform(proj4326,proj900913));
map.zoomTo(zoom);
//alert("testing here");
var wards = new OpenLayers.Layer.Vector("Wards poly", {
//strategies: [new OpenLayers.Strategy.BBOX({ratio:1})],
strategies: [new OpenLayers.Strategy.Fixed()],
projection: new OpenLayers.Projection('EPSG:4326'),
protocol: new OpenLayers.Protocol.HTTP({
url: "wards_osm_poly.osm", //<-- relative or absolute URL to your .osm file
format: new OpenLayers.Format.OSM()
})
});
var point = new OpenLayers.Layer.Vector("Wards point", {
//strategies: [new OpenLayers.Strategy.BBOX({ratio:1})],
strategies: [new OpenLayers.Strategy.Fixed()],
projection: new OpenLayers.Projection('EPSG:4326'),
protocol: new OpenLayers.Protocol.HTTP({
url: "wards_osm_point.osm", //<-- relative or absolute URL to your .osm file
format: new OpenLayers.Format.OSM()
})
});
map.addLayer(point);
map.addLayer(wards);
alert("Finished Loading");
var highlighter = new OpenLayers.Control.SelectFeature(wards,{
hover:true,
highlightOnly:true,
//eventListeners: {featurehighlighted: onHover}
});
map.addControl(highlighter);
highlighter.activate();
}
</script>