var coord1 = [-5707673.76, -3499420.81];
var coord2 = [-6707673.76, -3499420.81];
var lineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#ffcc33', width: 3 })
});
var styleMarker = new ol.style.Style({
image: new ol.style.Icon({
scale: .7, anchor: [0.5, 1],
src: '//raw.githubusercontent.com/jonataswalker/map-utils/master/images/marker.png'
})
});
var marker1 = new ol.geom.Point(coord1);
var featureMarker1 = new ol.Feature(marker1);
var marker2 = new ol.geom.Point(coord2);
var featureMarker2 = new ol.Feature(marker2);
var line = new ol.geom.LineString([coord1, coord2]);
var lineFeature = new ol.Feature(line);
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
features: [lineFeature, featureMarker1, featureMarker2]
}),
style: [lineStyle, styleMarker]
});
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), vector],
view: new ol.View({ center: coord1, zoom: 5 })
});
var translate1 = new ol.interaction.Translate({
features: new ol.Collection([featureMarker1])
});
var translate2 = new ol.interaction.Translate({
features: new ol.Collection([featureMarker2])
});
map.addInteraction(translate1);
map.addInteraction(translate2);
var coordMarker1, coordMarker2;
translate1.on('translatestart', function (evt) {
coordMarker2 = marker2.getCoordinates();
});
translate1.on('translating', function (evt) {
line.setCoordinates([coordMarker2, evt.coordinate]);
});
translate2.on('translatestart', function (evt) {
coordMarker1 = marker1.getCoordinates();
});
translate2.on('translating', function (evt) {
line.setCoordinates([coordMarker1, evt.coordinate]);
});
map.on('pointermove', function(e) {
if (e.dragging) return;
var hit = map.hasFeatureAtPixel(map.getEventPixel(e.originalEvent));
map.getTargetElement().style.cursor = hit ? 'pointer' : '';
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.