-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (85 loc) · 2.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
// Define source
var citiesSource = new ol.source.Vector()
// Define layer
var citiesLayer = new ol.layer.Vector({
source: citiesSource
// style: new ol.style.Style({
// fill: new ol.style.Fill({
// color: "red"
// })
// })
})
map.addLayer(citiesLayer);
$.ajax({
url: 'cities.php',
type: 'GET',
success: function (dataResult) {
var result = JSON.parse(dataResult);
// console.log(result);
result.forEach(element => {
var city = (new ol.format.GeoJSON()).readFeature(JSON.parse(element['geom']))
city.setProperties({ 'name': element['name'] })
citiesSource.addFeature(city)
})
}
})
function featinfo(evt) {
var clickedFeature = map.forEachFeatureAtPixel(evt.pixel,
function (feature) {
return feature
})
if (clickedFeature) {
document.getElementById('nameoffeature').innerText = clickedFeature.get('name')
$('#featureinfo').modal('show')
}
}
var coordOfClickedLoc
function addFeatures(evt) {
coordOfClickedLoc = evt.target.getCoordinateFromPixel(evt.pixel)
console.log(coordOfClickedLoc)
$('#addfeature').modal('show')
map.on('click', featinfo)
map.un('click', addFeatures)
}
// Getting information about a clicked feature
map.on('click', featinfo)
function addFeat() {
map.un('click', featinfo)
map.on('click', addFeatures)
}
function saveData() {
var cityName = document.getElementById('cityname').value
if (cityName == "") {
alert("Please enter city name")
} else {
$.ajax({
url: 'save_city.php',
type: 'POST',
data: {
name: cityName,
long: coordOfClickedLoc[0],
lat: coordOfClickedLoc[1]
},
success: function (dataResult) {
var result = JSON.parse(dataResult)
if (result.statusCode == 200) {
console.log('Location added to database successfully')
} else {
console.log('There is an error with the code!')
}
}
})
}
}