<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/ol3/3.8.2/ol.min.css" type="text/css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="map" tabindex="0"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/ol3/3.8.2/ol.min.js" type="text/javascript"></script>
<script src="script.js"></script>
</body>
</html>
var styles = {
'Point': [new ol.style.Style({
image: new ol.style.Circle({
radius: 8,
fill: new ol.style.Fill({
color: [255, 255, 255, 0.3]
}),
stroke: new ol.style.Stroke({color: '#cb1d1d', width: 2})
})
})],
'LineString': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 1
})
})],
'Polygon': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
lineDash: [4],
width: 3
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.1)'
})
})],
'Circle': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2
}),
fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.2)'
})
})]
};
var styleFunction = function(feature, resolution) {
return styles[feature.getGeometry().getType()];
};
var olview = new ol.View({
center: [-9343811.35, 5424617.38],
zoom: 13,
minZoom: 2,
maxZoom: 20
});
var geojson_layer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'file.geojson',
format: new ol.format.GeoJSON()
}),
style: styleFunction
});
var map = new ol.Map({
target: document.getElementById('map'),
view: olview,
layers: [
new ol.layer.Tile({
style: 'Aerial',
source: new ol.source.MapQuest({layer: 'sat'})
}), geojson_layer
]
});
/**
* Popup
**/
var
container = document.getElementById('popup'),
content_element = document.getElementById('popup-content'),
closer = document.getElementById('popup-closer');
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
var overlay = new ol.Overlay({
element: container,
autoPan: true,
offset: [0, -10]
});
map.addOverlay(overlay);
var fullscreen = new ol.control.FullScreen();
map.addControl(fullscreen);
map.on('click', function(evt){
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature, layer) {
return feature;
});
if (feature) {
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
var content = '<h3>' + feature.get('name') + '</h3>';
content += '<h5>' + feature.get('description') + '</h5>';
content_element.innerHTML = content;
overlay.setPosition(coord);
console.info(feature.getProperties());
}
});
map.on('pointermove', function(e) {
if (e.dragging) return;
var pixel = map.getEventPixel(e.originalEvent);
var hit = map.hasFeatureAtPixel(pixel);
map.getTarget().style.cursor = hit ? 'pointer' : '';
});
html, body, #map{
width:100%; height:100%;
overflow: hidden;
}
#map{
position:absolute;
z-index:1;
top:0; bottom:0;
}
.ol-popup {
position: absolute;
min-width: 180px;
background-color: white;
-webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
padding: 15px;
border-radius: 10px;
border: 1px solid #ccc;
bottom: 12px;
left: -50px;
}
.ol-popup:after, .ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.ol-popup:after {
border-top-color: white;
border-width: 10px;
left: 48px;
margin-left: -10px;
}
.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}
.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 2px;
right: 8px;
}
.ol-popup-closer:after {
content: "✖";
}
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "EPSG:4326"
}
},
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-83.9483,
43.7359
]
},
"properties": {
"name": "Hoyle's Marina-Linwood: 98-08-0324-P",
"description": "135 S Linwood Beach, Linwood"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-83.9387,
43.732
]
},
"properties": {
"name": "Mitchel/Pajot CI-Linwood: 98-08-0554-P",
"description": "43.732, -83.9387"
}
}
]
}