select control fit selected Feature to map extent (strange question...why would someone like to do that?)
by Thomas B.
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.11/OpenLayers.js"></script>
<body onload="init()">
select control fit selected Feature to map extent (strange question...why would someone like to do that?)
<div id="map"></div>
</body>
CSS
#map {
width: 600px;
height: 600px;
}
JavaScript
var apiKey = "AqTGBsziZHIJYYxgivLBf0hVdrAk9mWO5cQcb8Yux8sW5M8c8opEC2lZqKR1ZZXf";
var lon = -71.147;
var lat = 42.472;
var map, vlayer, selectFeature;
function init() {
map = new OpenLayers.Map('map', {
controls: [new OpenLayers.Control.PanZoom(), new OpenLayers.Control.Navigation()]
});
var road = new OpenLayers.Layer.Bing({
key: apiKey,
type: "Road",
metadataParams: {
mapVersion: "v1"
}
});
map.addLayers([road]);
vlayer = new OpenLayers.Layer.Vector("Editable");
map.addLayer(vlayer);
var zoom = map.getZoomForResolution(76.43702827453613);
map.setCenter(new OpenLayers.LonLat(lon, lat).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()), zoom);
selectControl = new OpenLayers.Control.SelectFeature(vlayer, {
onSelect: onFeatureSelect,
onUnselect: onFeatureUnselect
});
map.addControl(selectControl);
selectControl.activate();
loadPointsfromGeojson();
}
function onPopupClose(evt) {
selectControl.unselect(selectedFeature);
}
function onFeatureSelect(feature) {
// map.zoomToExtent(feature.geometry.getBounds()
// , closest=true
// );
fitToScale();
}
function onFeatureUnselect(feature) {
map.zoomTo(map.getZoom()-3);
}
function loadPointsfromGeojson() {
var featurecollection = {
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::3857"
}
},
"features": [{
"type": "Feature",
"properties": {
"name": "stoneham"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-7920271.1061104629, 5233681.5452001588],
[-7918608.6007452607,...