Skip to content

Instantly share code, notes, and snippets.

@rezkyal
Last active May 24, 2018 16:25
Show Gist options
  • Save rezkyal/26a0383e91195ec9dde764549918ea53 to your computer and use it in GitHub Desktop.
Save rezkyal/26a0383e91195ec9dde764549918ea53 to your computer and use it in GitHub Desktop.
var image = new ol.style.Circle({
radius: 5,
fill: null,
stroke: new ol.style.Stroke({color: 'red', width: 1})
});
var styles = {
'Point': new ol.style.Style({
image: image
}),
'LineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 1
})
}),
'MultiLineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
width: 6
})
}),
'MultiPoint': new ol.style.Style({
image: image
}),
'MultiPolygon': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'yellow',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(255, 255, 0, 0.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)'
})
}),
'GeometryCollection': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'magenta',
width: 2
}),
fill: new ol.style.Fill({
color: 'magenta'
}),
image: new ol.style.Circle({
radius: 10,
fill: null,
stroke: new ol.style.Stroke({
color: 'magenta'
})
})
}),
'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 styles1 = {
'Point': new ol.style.Style({
image: image
}),
'LineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 1
})
}),
'MultiLineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2
})
}),
'MultiPoint': new ol.style.Style({
image: image
}),
'MultiPolygon': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'yellow',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(255, 255, 0, 0.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)'
})
}),
'GeometryCollection': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'magenta',
width: 2
}),
fill: new ol.style.Fill({
color: 'magenta'
}),
image: new ol.style.Circle({
radius: 10,
fill: null,
stroke: new ol.style.Stroke({
color: 'magenta'
})
})
}),
'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)'
})
})
};
function CenterMap(long, lat) {
console.log("Long: " + long + " Lat: " + lat);
map.getView().setCenter(ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857'));
map.getView().setZoom(5);
}
var geojsonObject;
var styleFunction = function(feature) {
return styles[feature.getGeometry().getType()];
};
var styleFunction1 = function(feature) {
return styles1[feature.getGeometry().getType()];
};
var format = new ol.format.GeoJSON({
featureProjection:"EPSG:3857"
});
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: styleFunction
});
var vectorLayer1 = new ol.layer.Vector({
source: new ol.source.Vector(),
style: styleFunction1
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer,
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: {
collapsible: false
}
}),
view: new ol.View({
center: ol.proj.fromLonLat([112.7425, -7.265278]),
zoom: 12
})
});
//vectorLayer.getSource().clear();
if(vectorLayer.getSource()){
vectorLayer.getSource().clear();
}
// // if(vectorLayer1.getSource()){
// // vectorLayer1.getSource().clear();
// }
var format = new ol.format.GeoJSON({
featureProjection:"EPSG:3857"
});
var done= new Promise(function(resolve,reject){
// var elements=document.forms.lembar.getElementsByTagName('input');
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
var url="http://localhost/TKMI/cek.php"
oReq.open("GET",url, true);
oReq.send();
console.log(url);
function reqListener(e) {
geojsonObject = JSON.parse(this.responseText);
resolve(geojsonObject);
}
});
done.then((geojsonObject)=>{
console.log(geojsonObject);
//console.log(vectorLayer.getSource());
vectorLayer.getSource().addFeatures(format.readFeatures(geojsonObject));
// vectorLayer1.getSource().addFeatures(format.readFeatures(geojsonObject.dijkstra));
//console.log(vectorLayer.getSource());
// var vectorSource = new ol.source.Vector({
// features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
// });
//var geojsonObject = t;
//window.alert(geojsonObject);
}).catch((error)=>{
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment