Create Firs Map Code All
Create Firs Map Code All
Code :
<!doctype html>
<html>
<head>
<title>OpenLayers Components</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<div id="overlay" style="background-color: yellow; width: 20px; height: 20px;
border-radius: 10px;"></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
// create a layer with the OSM source
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
// finally, the map with our custom interactions, controls and overlays
var map = new ol.Map({
target: 'map',
layers: [layer],
interactions: [interaction],
controls: [control],
overlays: [overlay],
view: view
});
</script>
</body>
</html>
Create map
Code
<!doctype html>
<html>
<head>
<title>OpenLayers Components</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<div id="overlay" style="background-color: yellow; width: 20px; height: 20px;
border-radius: 10px;"></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
// create a layer with the OSM source
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
// finally, the map with our custom interactions, controls and overlays
var map = new ol.Map({
target: 'map',
layers: [layer],
interactions: [interaction],
controls: [control],
overlays: [overlay],
view: view
});
</script>
</body>
</html>
Overlaying information
Code:
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<input type="checkbox" id="visible" checked> Toggle Layer Visibility
<script src="../assets/ol3/js/ol.js"></script>
<script>
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6
});
var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});
// create a DOM Input helper for the checkbox
var visible = new ol.dom.Input(document.getElementById('visible'));
// and bind its 'checked' property to the layer's 'visible' property
visible.bindTo('checked', layer, 'visible');
</script>
</body>
</html>
Creating map class
Code:
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
// the normal setup for our samples
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6
});
var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});
</script>
</body>
</html>
Target practice
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div class="map">
<div id="right" class="half-map"></div>
<div id="left" class="half-map"></div>
<div class="panel">
<button onclick="changeTarget();">Change Target</button>
</div>
</div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6
});
var map = new ol.Map({
target: 'right',
layers: [layer],
view: view
});
function doBounce(location) {
// bounce by zooming out one level and back in
var bounce = ol.animation.bounce({
resolution: map.getView().getResolution() * 2
});
// start the pan at the current center of the map
var pan = ol.animation.pan({
source: map.getView().getCenter()
});
map.beforeRender(bounce);
map.beforeRender(pan);
// when we set the center to the new location, the animated move will
// trigger the bounce and pan effects
map.getView().setCenter(location);
}
function doPan(location) {
// pan from the current center
var pan = ol.animation.pan({
source: map.getView().getCenter()
});
map.beforeRender(pan);
// when we set the new location, the map will pan smoothly to it
map.getView().setCenter(location);
}
function doRotate() {
// rotate 360 degrees
var rotate = ol.animation.rotate({
rotation: Math.PI * 2
});
map.beforeRender(rotate);
}
function doZoom(factor) {
// zoom from the current resolution
var zoom = ol.animation.zoom({
resolution: map.getView().getResolution()
});
map.beforeRender(zoom);
// setting the resolution to a new value will smoothly zoom in or out
// depending on the factor
map.getView().setResolution(map.getView().getResolution() * factor);
}
</script>
</body>
</html>
Link to view
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map1" class="half-map"></div>
<div id="map2" class="half-map"></div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: [-201694.22, 6880651.07],
zoom: 6
});
// create two maps
var map1 = new ol.Map({
target: 'map1',
layers: [layer]
});
var map2 = new ol.Map({
target: 'map2',
layers: [layer],
view: view
});
// and bind the view properties so they effectively share a view
map1.bindTo('view', map2);
</script>
</body>
</html>
map.addLayer(osmLayer);
map.setView(view);
/*
console.log(osmLayer.getSource());
osmLayer.setProperties({opacity: 0.4, contrast:0.6});
console.log(osmLayer.get('contrast'));
console.log(osmLayer.get('opacity'));
osmLayer.setProperties({opacity: 0.7, contrast:0.3});
console.log(osmLayer.getOpacity());
console.log(osmLayer.getContrast());
osmLayer.set('opacity',1);
osmLayer.setContrast(1);
osmLayer.setBrightness(0);
osmLayer.set('myId', 'myUnique');
console.log(osmLayer.get('myId'));
*/
</script>
</body>
</html>
// Maps always need a projection, but Zoomify layers are not geo-referenced,
and
// are only measured in pixels. So, we create a fake projection that the map
// can use to properly display the layer.
var proj = new ol.proj.Projection({
code: 'ZOOMIFY',
units: 'pixels',
extent: [0, 0, imgWidth, imgHeight]
});
</script>
</body>
</html>
Playing with various sources and layers together
<!doctype html>
<html>
<head>
<title>Playing with various sources and layers</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
</script>
</body>
</html>
// this will be called when the server data has been loaded, we can
// use the source to read the features using its configured format
var loadFeatures = function(response) {
var features = vectorSource.readFeatures(response);
vectorSource.addFeatures(features);
};
Geometries in action
<!doctype html>
<html>
<head>
<title>Geometry Examples</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<div id="closestFeature"></div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
var countries = new ol.layer.Vector({
source: new ol.source.GeoJSON({
projection: 'EPSG:3857',
url: '../assets/data/countries.geojson'
})
});
var view = new ol.View({
projection: 'EPSG:3857'
});
var map = new ol.Map({
target: 'map',
layers: [countries],
view: view
});
// zoom to the extent of the vector source once the map has been rendered
// for the first time
map.once('postrender', function() {
view.fitExtent(countries.getSource().getExtent(), map.getSize());
});
function onMouseMove(browserEvent) {
// the mousemove event sends a browser event object that contains
// the geographic coordinate the event happened at
var coordinate = browserEvent.coordinate;
// we can get the closest feature from the source
var feature =
countries.getSource().getClosestFeatureToCoordinate(coordinate);
// to compute the area of a feature, we need to get it's geometry and do
// something a little different depeneding on the geometry type.
var geometry = feature.getGeometry();
var area;
switch (geometry.getType()) {
case 'MultiPolygon':
// for multi-polygons, we need to add the area of each polygon
area = geometry.getPolygons().reduce(function(left, right) {
return left + right.getArea();
}, 0);
break;
case 'Polygon':
// for polygons, we just get the area
area = geometry.getArea();
break;
default:
// no other geometry types have area as far as we are concerned
area = 0;
}
area = area / 1000000;
// display the country name and area now
var text = feature.getProperties().name + ' ' + area.toFixed(0) + '
km<sup>2</sup>';
document.getElementById('closestFeature').innerHTML = text;
}
</script>
</body>
</html>
// when the user moves the mouse, get the name property
// from each feature under the mouse and display it
function onMouseMove(browserEvent) {
var coordinate = browserEvent.coordinate;
var pixel = map.getPixelFromCoordinate(coordinate);
var el = document.getElementById('name');
el.innerHTML = '';
map.forEachFeatureAtPixel(pixel, function(feature) {
el.innerHTML += feature.get('name') + '<br>';
});
}
map.on('pointermove', onMouseMove);
</script>
</body>
</html>
Basic styling
<!doctype html>
<html>
<head>
<title>Vector Style Examples</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
// a basic style for the country layer
var countryStyle = new ol.style.Style({
fill: new ol.style.Fill({
color: [203, 194, 185, 1]
}),
stroke: new ol.style.Stroke({
color: [177, 163, 148, 0.5],
width: 2,
lineCap: 'round'
})
});
earthquakes.once('render', function() {
view.fitExtent(earthquakes.getSource().getExtent(), map.getSize());
});
</script>
</body>
</html>
// we want to merge the country data with the income level data. After
// the country data is 'ready', we can load the income level data and
// the add a new property by linking the two sets of data on the
// ISO country code that is present in both data sets.
var key = source.on('change', function(event) {
if (source.getState() == 'ready') {
source.unByKey(key);
$.ajax('../assets/data/income_levels.json').done(function(data) {
countries.getSource().forEachFeature(function(feature) {
var code = feature.get('iso_a2');
if (data[code]) {
feature.set('incomeLevel', data[code]);
}
});
});
}
});
</script>
</body>
</html>
// when we move the mouse over a feature, we can change its style to
// highlight it temporarily
var highlightStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: [255,0,0,0.6],
width: 2
}),
fill: new ol.style.Fill({
color: [255,0,0,0.2]
}),
zIndex: 1
});
return [style];
}
// when the mouse moves over the map, we get an event that we can use
// to create a new feature overlay from
map.on('pointermove', function(browserEvent) {
// first clear any existing features in the overlay
featureOverlay.getFeatures().clear();
var coordinate = browserEvent.coordinate;
var pixel = browserEvent.pixel;
// then for each feature at the mouse position ...
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
// check the layer property, if it is not set then it means we
// are over an OverlayFeature and we can ignore this feature
if (!layer) {
return;
}
// test the feature's geometry type and compute a reasonable point
// at which to display the text.
var geometry = feature.getGeometry();
var point;
switch (geometry.getType()) {
case 'MultiPolygon':
var poly = geometry.getPolygons().reduce(function(left, right) {
return left.getArea() > right.getArea() ? left : right;
});
point = poly.getInteriorPoint().getCoordinates();
break;
case 'Polygon':
point = geometry.getInteriorPoint().getCoordinates();
break;
default:
point = geometry.getClosestPoint(coordinate);
}
// create a new feature to display the text
textFeature = new ol.Feature({
geometry: new ol.geom.Point(point),
text: feature.get('name'),
isoCode: feature.get('iso_a2').toLowerCase()
});
// and add it to the featureOverlay. Also add the feature itself
// so the country gets outlined
featureOverlay.addFeature(textFeature);
featureOverlay.addFeature(feature);
});
});
</script>
</body>
</html>
map.addLayer(blueMarbleLayer);
map.setView(view);
</script>
</body>
</html>
<script src="../assets/proj4js/proj4.js"></script>
<script src="../assets/ol3/js/ol.js"></script>
<script>
var layers = [
new ol.layer.Image({
source: new ol.source.ImageWMS({
url: 'http://geoservices.brgm.fr/geologie',
attributions: [new ol.Attribution({
html: '© ' +
'BRGM (French USGS equivalent)'
})
],
params: {
'LAYERS': 'SCAN_F_GEOL1M',
'VERSION': '1.1.1'
},
extent: extent
})
})
];
var layers = [
new ol.layer.Image({
source: new ol.source.ImageWMS({
url: 'http://geoservices.brgm.fr/geologie',
attributions: [new ol.Attribution({
html: '© ' +
'BRGM (French USGS equivalent)'
})
],
params: {
'LAYERS': 'SCAN_F_GEOL1M',
'VERSION': '1.1.1'
}
})
})
];
<script src="../assets/proj4js/proj4.js"></script>
<script src="../assets/ol3/js/ol.js"></script>
<script>
countriesSource.once('change', function(evt) {
if (this.getState() == 'ready') {
console.log(this.getFeatures()[0].getGeometry().getCoordinates());
console.log(this.getFeatures()
[0].getGeometry().clone().transform('EPSG:2154','EPSG:4326').getCoordinates());
}
});
var layers = [
new ol.layer.Image({
source: new ol.source.ImageWMS({
url: 'http://geoservices.brgm.fr/geologie',
attributions: [new ol.Attribution({
html: '© ' +
'BRGM (French USGS equivalent)'
})
],
params: {
'LAYERS': 'SCAN_F_GEOL1M',
'VERSION': '1.1.1'
},
extent: extent
})
}),
new ol.layer.Vector({
source: countriesSource
})
];
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[[-0.944824, 46.134170], [-0.944824, 48.312428],
[4.438477, 48.312428], [4.438477, 46.134170],
[-0.944824, 46.134170]
]
]
}
}
]
};
</script>
</body>
</html>
map.addLayer(raster);
map.addLayer(vectorEuropa);
map.setView(view);
map.getInteractions().extend([selectInteraction]);
</script>
</body>
</html>
map.addLayer(raster);
map.addLayer(vectorEuropa);
map.addLayer(vectorFrancePoints);
map.setView(view);
map.getInteractions().extend([selectInteraction]);
vectorEuropa.set('selectable', true);
vectorFrancePoints.set('selectable', true);
</script>
</body>
</html>
Understanding
forEachFeatureAtPixel method
<!doctype html>
<html>
<head>
<title>Get Features from vector</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="information"></div>
<div id="map" class="map"></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
map.addLayer(raster);
map.addLayer(vectorEuropa);
map.setView(view);
map.getInteractions().extend([selectInteraction]);
map.on('click', function(evt) {
var pixel = evt.pixel;
displayFeatureInfo(pixel);
});
</script>
</body>
</html>
map.on('click', function(evt) {
var url = wms_layer.getSource().getGetFeatureInfoUrl(
evt.coordinate, viewResolution, viewProjection,
{'INFO_FORMAT': 'text/javascript',
'propertyName': 'formal_en'});
if (url) {
var parser = new ol.format.GeoJSON();
$.ajax({
url: url,
dataType: 'jsonp',
jsonpCallback: 'parseResponse'
}).then(function(response) {
var result = parser.readFeatures(response);
if (result.length) {
var info = [];
for (var i = 0, ii = result.length; i < ii; ++i) {
info.push(result[i].get('formal_en'));
}
container.innerHTML = info.join(', ');
} else {
container.innerHTML = ' ';
}
});
}
});
</script>
</body>
</html>
map.addLayer(osmLayer);
map.setView(view);
map.addOverlay(popup);
popup.setPosition(ol3_sprint_location);
</script>
</body>
</html>
map.addLayer(osmLayer);
map.addLayer(vectorEuropa);
map.setView(view);
map.addOverlay(popup);
function pickRandomProperty() {
var prefix = ['bottom', 'center', 'top'];
var randPrefix = prefix[Math.floor(Math.random() * prefix.length)];
var suffix = ['left', 'center', 'right'];
var randSuffix = suffix[Math.floor(Math.random() * suffix.length)];
return randPrefix + '-' + randSuffix;
}
map.on('click', function(evt) {
var coordinate = evt.coordinate;
displayFeatureInfo(evt.pixel, coordinate);
});
</script>
</body>
</html>
typeSelect.onchange = function(e) {
map.removeInteraction(draw);
addInteraction();
};
addInteraction();
</script>
</body>
</html>
map.addLayer(raster);
map.addLayer(vectorEuropa);
map.setView(view);
map.getInteractions().extend([selectInteraction, modify]);
selected_features.on('add', function(evt) {
var feature = evt.element;
var fid = feature.getId();
feature.on('change', function(evt) {
dirty[evt.target.getId()] = true;
});
});
Using ol.interaction.DragRotateAndZoom
<!doctype html>
<head>
<title>Simple example</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
var osm_default = new ol.layer.Tile({
source: new ol.source.OSM()
});
dragBoxInteraction.on('boxend', function(e) {
var format = new ol.format.GeoJSON();
var geom = e.target.getGeometry();
geom.transform('EPSG:3857', 'EPSG:4326');
var feature = new ol.Feature({
geometry: geom
});
var obj = format.writeFeatures([feature]);
console.log(JSON.stringify(obj));
});
</script>
</body>
</html>
map.addControl(mousePosition);
</script>
</body>
</html>
map.addControl(mousePosition);
</script>
</body>
</html>
map.addControl(zoomToExtentControl);
var controls = map.getControls();
var attributionControl;
controls.forEach(function (el) {
console.log(el instanceof ol.control.Attribution);
if (el instanceof ol.control.Attribution) {
attributionControl = el;
}
});
map.removeControl(attributionControl);
</script>
</body>
</html>
window.app = {};
var app = window.app;
/**
* @constructor
* @extends {ol.control.Control}
* @param {Object=} opt_options Control options.
*/
app.generateGeoJSONControl = function(opt_options) {
var options = opt_options || {};
var anchor = document.createElement('a');
anchor.href = '#export-geojson';
anchor.innerHTML = 'G';
ol.control.Control.call(this, {
element: element,
target: options.target
});
};
ol.inherits(app.generateGeoJSONControl, ol.control.Control);
var draw;
function addInteraction() {
draw = new ol.interaction.Draw({
source: source,
type: typeSelect.value
});
map.addInteraction(draw);
}
typeSelect.onchange = function(e) {
map.removeInteraction(draw);
addInteraction();
};
addInteraction();
</script>
</body>
</html>
Go mobile!
<!doctype html>
<html>
<head>
<title>Mobile Examples</title>
<!-- the viewport is important for mobile devices -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-
scale=1.0, user-scalable=no">
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="full-map"></div>
<script src="../assets/ol3/js/ol.js"></script>
<script>
// other than adding the <meta> viewport above, our normal setup will
// work fine for mobile
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6
});
var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});
</script>
</body>
</html>
Location
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-
scale=1.0, user-scalable=no">
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
<!-- font-awesome is an iconic font, which means we can draw resolution-
independent icons -->
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css"
rel="stylesheet">
</head>
<body>
<div id="map" class="full-map"></div>
<div id="location" class="marker"><span class="icon-flag"></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
// bind the projection to the view so that positions are reported in the
// projection of the view
geolocation.bindTo('projection', view);
// bind the marker's position to the geolocation object, the marker will
// move automatically when the GeoLocation API provides position updates
marker.bindTo('position', geolocation);
// when the GeoLocation API provides a position update, center the view
// on the new position
geolocation.on('change:position', function() {
var p = geolocation.getPosition();
console.log(p[0] + ' : ' + p[1]);
view.setCenter([parseFloat(p[0]), parseFloat(p[1])]);
});
</script>
</body>
</html>
A sense of direction
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-
scale=1.0, user-scalable=no">
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css"
rel="stylesheet">
</head>
<body>
<div id="map" class="full-map"></div>
<div id="location" class="marker"><span class="icon-arrow-up"></span></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6
});
var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});
// set up geolocation to track our position
var geolocation = new ol.Geolocation({
tracking: true
});
// bind it to the view's projection and update the view as we move
geolocation.bindTo('projection', view);
geolocation.on('change:position', function() {
view.setCenter(geolocation.getPosition());
});
// add a marker to display the current location
var marker = new ol.Overlay({
element: document.getElementById('location'),
positioning: 'center-center'
});
map.addOverlay(marker);
// and bind it to the geolocation's position updates
marker.bindTo('position', geolocation);
Track me
<!doctype html>
<html>
<head>
<title>Map Examples</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-
scale=1.0, user-scalable=no">
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css"
rel="stylesheet">
</head>
<body>
<div id="map" class="full-map"></div>
<div id="location" class="marker"><span class="icon-arrow-up"></span></div>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
// create a style to display our position history (track)
var trackStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0,0,255,1.0)',
width: 3,
lineCap: 'round'
})
});
// use a single feature with a linestring geometry to display our track
var trackFeature = new ol.Feature({
geometry: new ol.geom.LineString([])
});
// we'll need a vector layer to render it
var trackLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [trackFeature]
}),
style: trackStyle
});
var baseLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326',
'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6
});
var map = new ol.Map({
target: 'map',
layers: [baseLayer, trackLayer],
view: view
});
// set up the geolocation api to track our position
var geolocation = new ol.Geolocation({
tracking: true
});
// bind the view's projection
geolocation.bindTo('projection', view);
// when we get a position update, add the coordinate to the track's
// geometry and recenter the view
geolocation.on('change:position', function() {
var coordinate = geolocation.getPosition();
view.setCenter(coordinate);
trackFeature.getGeometry().appendCoordinate(coordinate);
});
// put a marker at our current position
var marker = new ol.Overlay({
element: document.getElementById('location'),
positioning: 'center-center'
});
map.addOverlay(marker);
marker.bindTo('position', geolocation);
// rotate the view to match the device orientation
var deviceOrientation = new ol.DeviceOrientation({
tracking: true
});
deviceOrientation.on('change:heading', onChangeHeading);
function onChangeHeading(event) {
var heading = event.target.getHeading();
view.setRotation(-heading);
}
</script>
</body>
</html>
</script>
</body>
</html>
</script>
</body>
</html>
function flickrStyle(feature) {
var style = new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
stroke: new ol.style.Stroke({
color: 'white',
width: 2
}),
fill: new ol.style.Fill({
color: 'green'
})
})
});
return [style];
}
// when jQuery has loaded the data, we can create features for each photo
function successHandler(data) {
// we need to transform the geometries into the view's projection
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
// loop over the items in the response
data.items.forEach(function(item) {
// create a new feature with the item as the properties
var feature = new ol.Feature(item);
// add a url property for later ease of access
feature.set('url', item.media.m);
// create an appropriate geometry and add it to the feature
var coordinate = transform([parseFloat(item.longitude),
parseFloat(item.latitude)]);
var geometry = new ol.geom.Point(coordinate);
feature.setGeometry(geometry);
// add the feature to the source
flickrSource.addFeature(feature);
});
}
$.ajax({
url: '../assets/data/flickr_data.json',
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed',
success: successHandler
});
</script>
</body>
</html>
function flickrStyle(feature) {
// cache styles by photo url
var url = feature.get('url');
if (!cache[url]) {
// use the icon style and scale the image to 10% so its not too large
cache[url] = new ol.style.Style({
image: new ol.style.Icon({
scale: 0.10,
src: url
})
});
}
return [cache[url]];
}
function successHandler(data) {
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
data.items.forEach(function(item) {
var feature = new ol.Feature(item);
feature.set('url', item.media.m);
var coordinate = transform([parseFloat(item.longitude),
parseFloat(item.latitude)]);
var geometry = new ol.geom.Point(coordinate);
feature.setGeometry(geometry);
flickrSource.addFeature(feature);
});
}
$.ajax({
url: '../assets/data/flickr_data.json',
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed',
success: successHandler
});
</script>
</body>
</html>
map.addInteraction(select);
</script>
</body>
</html>
Handling selection events
<!doctype html>
<html>
<head>
<title>Flickr Search</title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
</head>
<body>
<div id="map" class="map"></div>
<div id="photo-info"></div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="../assets/ol3/js/ol-debug.js"></script>
<script>
function flickrStyle(feature) {
return [photoStyle(feature, 0.10)];
}
function selectedStyle(feature) {
return [photoStyle(feature, 0.50)];
}
function successHandler(data) {
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
data.items.forEach(function(item) {
var feature = new ol.Feature(item);
feature.set('url', item.media.m);
var coordinate = transform([parseFloat(item.longitude),
parseFloat(item.latitude)]);
var geometry = new ol.geom.Point(coordinate);
feature.setGeometry(geometry);
flickrSource.addFeature(feature);
});
}
$.ajax({
url: '../assets/data/flickr_data.json',
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed',
success: successHandler
});
</script>
</body>
</html>
function flickrStyle(feature) {
return [photoStyle(feature, 0.10)];
}
function selectedStyle(feature) {
return [photoStyle(feature, 0.50)];
}
map.addInteraction(select);
selectedFeatures.on('add', function(event) {
var feature = event.target.item(0);
// instead of the photo url, get the templated content
// for the current feature instead
var content = photoContent(feature);
$('#photo-info').html(content);
});
selectedFeatures.on('remove', function(event) {
$('#photo-info').empty();
});
function successHandler(data) {
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
data.items.forEach(function(item) {
var feature = new ol.Feature(item);
feature.set('url', item.media.m);
var coordinate = transform([parseFloat(item.longitude),
parseFloat(item.latitude)]);
var geometry = new ol.geom.Point(coordinate);
feature.setGeometry(geometry);
flickrSource.addFeature(feature);
});
}
$.ajax({
url: '../assets/data/flickr_data.json',
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed',
success: successHandler
});
</script>
<!-- script tags are ignored for rendering, and adding type="text/html"
to the script tag makes the javascript engine ignore it, but
we can still access it as a DOM element and get its formatted content -->
<script type="text/html" id="photo-template">
<a href="{link}" target="_blank" title="Click to open photo in new tab"><img
src="{url}" style="float: left"></a><br>
<p>Taken by <a href="http://www.flickr.com/people/{author_id}" target="_blank"
title="Click to view author details in new tab">{author}</a> on {date_taken} at lat:
{latitude} lon: {longitude}</p><br>
<p>Tagged in <b>{tags}</b></p>
</script>
</body>
</html>
function flickrStyle(feature) {
return [photoStyle(feature, 0.10)];
}
function selectedStyle(feature) {
return [photoStyle(feature, 0.50)];
}
function photoContent(feature) {
var content = $('#photo-template').html();
var keys =
['author','author_id','date_taken','latitude','longitude','link','url','tags','title'
];
for (var i=0; i<keys.length; i++) {
var key = keys[i];
var value = feature.get(key);
content = content.replace('{'+key+'}',value);
}
return content;
}
map.addInteraction(select);
selectedFeatures.on('add', function(event) {
var feature = event.target.item(0);
var content = photoContent(feature);
$('#photo-info').html(content);
});
selectedFeatures.on('remove', function(event) {
$('#photo-info').empty();
});
function successHandler(data) {
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
data.items.forEach(function(item) {
var feature = new ol.Feature(item);
feature.set('url', item.media.m);
var coordinate = transform([parseFloat(item.longitude),
parseFloat(item.latitude)]);
var geometry = new ol.geom.Point(coordinate);
feature.setGeometry(geometry);
flickrSource.addFeature(feature);
});
}
// the only change is to point at the remote URL for the feed
$.ajax({
url: 'http://api.flickr.com/services/feeds/geo/?format=json&tags=bird',
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed',
success: successHandler
});
</script>
</body>
</html>
function flickrStyle(feature) {
return [photoStyle(feature, 0.10)];
}
function selectedStyle(feature) {
return [photoStyle(feature, 0.50)];
}
function photoContent(feature) {
var content = $('#photo-template').html();
var keys =
['author','author_id','date_taken','latitude','longitude','link','url','tags','title'
];
for (var i=0; i<keys.length; i++) {
var key = keys[i];
var value = feature.get(key);
content = content.replace('{'+key+'}',value);
}
return content;
}
map.addInteraction(select);
selectedFeatures.on('add', function(event) {
var feature = event.target.item(0);
var content = photoContent(feature);
$('#photo-info').html(content);
});
selectedFeatures.on('remove', function(event) {
$('#photo-info').empty();
});
function successHandler(data) {
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
data.items.forEach(function(item) {
var feature = new ol.Feature(item);
feature.set('url', item.media.m);
var coordinate = transform([parseFloat(item.longitude),
parseFloat(item.latitude)]);
var geometry = new ol.geom.Point(coordinate);
feature.setGeometry(geometry);
flickrSource.addFeature(feature);
});
}
// by moving the data loading into a function, we can call it with the user's
// search term and construct a new URL getting photos for the relevant tags.
// we can also clear existing features and selected features.
function loadFlickrFeed(tags) {
selectedFeatures.clear();
flickrSource.clear();
$('#photo-info').empty();
$.ajax({
url: 'http://api.flickr.com/services/feeds/geo',
data: {
format: 'json',
tags: tags
},
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed',
success: successHandler
});
}
</script>
Situsnya
https://openlayersbook.github.io/appendix-c-squashing-bugs-with-
web-debuggers/intro.html