-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_draw.html
179 lines (157 loc) · 5.98 KB
/
google_draw.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=drawing"></script>
<!--Global variables!-->
<script>
//This variable gets all coordinates of polygone and save them. Finally you should use this array because it contains all latitude and longitude coordinates of polygon.
var coordinates = [];
var map = '';
//This variable saves polygon.
var polygons = [];
var markers = [];
</script>
<script>
//This function save latitude and longitude to the polygons[] variable after we call it.
function save_coordinates_to_array(polygon)
{
//Save polygon to 'polygons[]' array to get its coordinate.
polygons.push(polygon);
//This variable gets all bounds of polygon.
var polygonBounds = polygon.getPath();
var cord = '';
document.getElementById("cord").innerHTML = '';
for(var i = 0 ; i < polygonBounds.length ; i++)
{
var marker = new google.maps.Marker({
position: new google.maps.LatLng(polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()),
map: map
});
markers.push(marker);
cord += ' Lati : ' + polygonBounds.getAt(i).lat() + ' Longi : ' + polygonBounds.getAt(i).lng() + ',';
document.getElementById("cord").innerHTML = cord;
coordinates.push(polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng());
//alert(i);
}
}
</script>
<script>
var selectedShape = '';
var dbMapPoints = [
{Pk_Id: 1, Fk_Id: 1, lat: 1.4205240044069118, lng:103.79196166992188},
{Pk_Id: 2, Fk_Id: 1, lat: 1.3995876582668938, lng:103.88362884521484},
{Pk_Id: 3, Fk_Id: 1, lat: 1.3371207388169672, lng:103.80226135253906},
{Pk_Id: 4, Fk_Id: 1, lat: 1.3721298722592206, lng:103.74561309814453},
{Pk_Id: 4, Fk_Id: 1, lat: 1.4136596493903157, lng:103.74561309814453}
];
var map = null;
var mapDefaults = {
zoom: 12,
center: new google.maps.LatLng(1.37584, 103.829)
};
function initialize()
{
//Create a Google maps.
map = new google.maps.Map(document.getElementById('map'), mapDefaults);
polyOptions = {
strokeWeight: 3,
fillOpacity: 0.45,
fillColor : '#008000',
strokeColor : '#008000',
editable: true
};
//Create a drawing manager panel that lets you select polygon, polyline, circle, rectangle or etc and then draw it.
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControlOptions: {
drawingModes: [
google.maps.drawing.OverlayType.POLYGON
]
},
polygonOptions: polyOptions
});
drawingManager.setMap(map);
//This event fires when creation of polygon is completed by user.
google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) {
//This line make it possible to edit polygon you have drawed.
polygon.setEditable(true);
selectedShape = polygon;
//Call function to pass polygon as parameter to save its coordinated to an array.
save_coordinates_to_array(polygon);
//This event is inside 'polygoncomplete' and fires when you edit the polygon by moving one of its anchors.
google.maps.event.addListener(polygon.getPath(), 'set_at', function () {
alert('changed');
save_coordinates_to_array(polygon);
});
//This event is inside 'polygoncomplete' too and fires when you edit the polygon by moving on one of its anchors.
google.maps.event.addListener(polygon.getPath(), 'insert_at', function () {
alert('also changed');
save_coordinates_to_array(polygon);
});
drawingManager.setDrawingMode(null);
});
google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
loadWithFk(1);
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
if (markers)
for (var i in markers)
{
markers[i].setMap(null);
}
}
function loadWithFk(fkId) {
var latlng = [];
if(fkId != '' && fkId != 0)
{
$.when($.each(dbMapPoints, function () {
latlng.push(new google.maps.LatLng(this.lat, this.lng));
var marker = new google.maps.Marker({
position: new google.maps.LatLng(this.lat, this.lng),
map: map
});
markers.push(marker);
})).done(function () {
mapPoly = new google.maps.Polygon({
paths: latlng,
strokeWeight: 3,
fillOpacity: 0.45,
fillColor : '#008000',
strokeColor : '#008000',
strokeOpacity: 0.5,
suppressInfoWindows: true,
clickable: false,
});
mapPoly.setMap(map);
});
}
else if(fkId == 0)
{
mapPoly.setMap(null);
}
}
function clearMap()
{
loadWithFk(0);
if (markers)
for (var i in markers)
{
markers[i].setMap(null);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<form>
<!--<input type="button" id="delete-button" onclick="deleteMap()" value="Delete Selected Shape"/>-->
<input type="button" id="delete-button" value="Show This Button On Add Form Delete Selected Shape"/>
<input type="button" id="delete-edit-button" onclick="clearMap()" value="Show This Button On EDIT Form Delete Selected Shape"/>
<div id="cord"></div>
<div style="width:1000px; height:400px;" id="map"></div>
</form>
</body>
</html>