openlayers_map.js Example File
webmapview/assets/openlayers_map.js
var map;
var markers;
function initMaps() {
console.log("init OpenLayers");
var myLat = 43.449766;
var myLong = -80.406096;
map = new OpenLayers.Map('map_canvas');
layer = new OpenLayers.Layer.OSM("Simple OSM Map");
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(myLong, myLat).transform(
new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()),
14);
map.events.register("move", map, centerChanged);
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 : clicked
});
var click = new OpenLayers.Control.Click();
map.addControl(click);
click.activate();
markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
console.log("Done init OpenLayers");
}
function clicked(e) {
var location = map.getLonLatFromPixel(e.xy);
navigator.cascades.postMessage("clicked:" + location.lat + ","
+ location.lon + "," + e.xy.x + ","
+ e.xy.y);
}
function centerChanged() {
var location = map.getCenter();
navigator.cascades.postMessage("centerChanged:" + location.lat + ","
+ location.lon + "," + map.getPixelFromLonLat(location).x + ","
+ map.getPixelFromLonLat(location).y);
}
function onMessage(message) {
}
function setZoom(zoomLevel) {
map.zoomTo(zoomLevel);
}
function zoomIn() {
map.zoomIn();
}
function zoomOut() {
map.zoomOut();
}
function setCenter(lat, lon) {
var lonlat = new OpenLayers.LonLat(lon, lat);
lonlat.transform(new OpenLayers.Projection("EPSG:4326"), map
.getProjectionObject());
map.setCenter(lonlat);
}
function setMapTypeId(mapType) {
}
function createPushPin(lat, lon, title, iconpath) {
var size = new OpenLayers.Size(60,60);
var offset = new OpenLayers.Pixel(20,60);
var icon = new OpenLayers.Icon(iconpath,size,offset);
var marker = new OpenLayers.Marker(new OpenLayers.LonLat(lon,lat), icon);
marker.events.register("click", marker, markerClicked);
markers.addMarker(marker);
}
function removeAllPins() {
markers.clearMarkers();
}
function markerClicked(e) {
window.alert("Hello");
var pinLoc = map.getLonLatFromPixel(e.xy);
navigator.cascades.postMessage("markerClicked:" + pinLoc.lat + "," + pinLoc.lon + "," + e.xy.x + "," + e.xy.y);
}