Skip to content

Instantly share code, notes, and snippets.

@tmcw tmcw/gist:158842
Created Jul 30, 2009

Embed
What would you like to do?
var kml_layers = new Array();
/**
* Process KML Layers
*
* @param layerOptions
* Object of options
* @param mapid
* Map ID
* @return
* Valid OpenLayers layer
*/
OL.Layers.KMLHTTP = function(layerOptions, mapid) {
var mapOptions = {
projection: new OpenLayers.Projection("EPSG:4326"),
format: OpenLayers.Format.KML,
strategies: [new OpenLayers.Strategy.BBOX()],
formatOptions: {extractStyles: true, extractAttributes: true},
protocol: new OpenLayers.Protocol.HTTP({
url: layerOptions.url,
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: true})
})
};
jQuery.extend(mapOptions, layerOptions.options);
var returnKML = new OpenLayers.Layer.Vector(
layerOptions.name,
mapOptions
);
kml_layers.push(returnKML);
return returnKML;
}
var blockUnselect = false;
function onPopupClose(evt) {
select.unselect(selectedFeature);
}
function onFeatureUnselect(feature) {
if(!blockUnselect && null != feature.popup) {
feature.layer.map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
}
function onFeatureSelect(feature) {
selectedFeature = feature;
text = "<div class='febp_ol_popup_name'>"+feature.attributes.name+"</div>";
popup = new OpenLayers.Popup.Anchored(
"views_kml_openlayers",
feature.geometry.getBounds().getCenterLonLat(),
null,
text,
feature.marker,
false, onPopupClose);
popup.setBackgroundColor('#ffffff');
popup.setBorder('1px solid #A7B9C4');
popup.autoSize = true;
$(popup.contentDiv).hover(
function() {
blockUnselect = true;
},
function() {
blockUnselect = false;
onFeatureUnselect(selectedFeature)
});
feature.popup = popup;
feature.layer.map.addPopup(popup);
}
String.prototype.unescapeHtml = function () {
var temp = document.createElement("div");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild)
return result;
}
function makeSelect(kml_layers){
return new OpenLayers.Control.SelectFeature(
kml_layers, // Apply to all feature layers
{
activeByDefault: true,
onSelect: onFeatureSelect,
highlightOnly: false,
onUnselect: onFeatureUnselect,
clickFeature: onClickFeature,
multiple: false,
hover: true,
callbacks: {
'over': onFeatureSelect,
'out': onFeatureUnselect
}
}
);
}
function onClickFeature(feature) {
// Here we need to unescape html escaped by Drupal's check_plain()
// function. This is a hack and may need to be improved
window.location = $("<div>"+feature.attributes.name+"</div>").find('a').attr('href');
}
OL.Behaviors.olhover = function(event) {
var mapDef = event.mapDef;
var mapid = mapDef.id;
var map = event.map;
var behavior = event.behavior;
var layer = OL.maps[mapid].layers[behavior.layer];
select_feature = new OpenLayers.Control.SelectFeature(
kml_layers, // Apply to all feature layers
{
activeByDefault: true,
onSelect: onFeatureSelect,
highlightOnly: false,
onUnselect: onFeatureUnselect,
clickFeature: onClickFeature,
multiple: false,
hover: true,
callbacks: {
'over': onFeatureSelect,
'out': onFeatureUnselect
}
}
);
OL.maps[mapid].controls[behavior.id] = select_feature;
// Add control
map.addControl(OL.maps[mapid].controls[behavior.id]);
OL.maps[mapid].controls[behavior.id].activate();
}
function refresh_refresh(event) {
console.log(event);
}
OL.Behaviors.refresh = function(event) {
var mapDef = event.mapDef;
var mapid = mapDef.id;
var map = event.map;
var behavior = event.behavior;
var layer = OL.maps[mapid].layers[behavior.layer];
var refresh_button = new OpenLayers.Control.Button({
displayClass: 'refreshButton', trigger: refresh_refresh});
var panel = new OpenLayers.Control.Panel();
panel.addControls(refresh_button);
OL.maps[mapid].controls[behavior.id] = panel;
// Add control
map.addControl(OL.maps[mapid].controls[behavior.id]);
OL.maps[mapid].controls[behavior.id].activate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.