google maps cirkel
Ik wil dat de cirkel van kleur veranderd als de waarde van updateMarkerAddress ' onbekend' is. Bij regel 200 gaat het mis.
Kan iemand me op weg helpen?
Code (php)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// global vars, we need them all over the app
var distanceWidget;
var radiusWidget;
var map;
var geocoder;
var perimeterMin = 5;
var perimeterMax = 100;
var perimeterDefault = 30;
function sliderIcon() {
return new google.maps.MarkerImage(
'Images/resize-off.png',
// size
new google.maps.Size(16, 16),
// The origin for this image
new google.maps.Point(0,0),
// The anchor for this image
new google.maps.Point(8, 8)
);
}
function dartIcon() {
return new google.maps.MarkerImage(
'Images/icon_dart4.png',
// size
new google.maps.Size(47, 41),
// The origin for this image
new google.maps.Point(0,0),
// The anchor for this image
new google.maps.Point(24,24)
);
}
$(document).ready(function(){
geocoder = new google.maps.Geocoder();
var center = new google.maps.LatLng(52.093271,5.121850);
var zoom = 8;
var mapDiv;
function init() {
mapDiv = document.getElementById('mapcanvas');
map = new google.maps.Map(mapDiv, {
center: center,
zoom: zoom,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
distanceWidget = new DistanceWidget(map, updateMarkerAddress);
google.maps.event.addListener(distanceWidget, 'distance_changed', function(e) {
display();
//$("#controls #perimeter_distance").val(radiusWidget.distance);
});
google.maps.event.addListener(distanceWidget, 'position_changed', function(e) {
//geocodePosition(distanceWidget.position);
display();
//$("#controls #perimeter_center").val(distanceWidget.position);
});
display();
geocodePosition(center);
}
google.maps.event.addDomListener(window, 'load', init);
});
function display() {
$("#controls #perimeter_distance_html").val(radiusWidget.distance);
$("#controls #perimeter_distance_value").html(radiusWidget.distance);
$("#controls #perimeter_center_lat").val(distanceWidget.position.lat().toString() );
$("#controls #perimeter_center_lng").val(distanceWidget.position.lng().toString() );
$("#controls #perimeter_north").val(radiusWidget.circle.getBounds().getNorthEast().lat());
$("#controls #perimeter_east").val(radiusWidget.circle.getBounds().getNorthEast().lng());
$("#controls #perimeter_south").val(radiusWidget.circle.getBounds().getSouthWest().lat());
$("#controls #perimeter_west").val(radiusWidget.circle.getBounds().getSouthWest().lng());
}
function updateMarkerAddress(str) {
$("#controls #perimeter_postal").val(str);
}
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
var city = addresComponent('locality', responses[0])
var postal_code = addresComponent('postal_code', responses[0])
if (postal_code) {
updateMarkerAddress(postal_code);
}
else {
updateMarkerAddress('Onbekend');
}
}
else {
updateMarkerAddress('Onbekend');
}
if (city) {
updateMarkerCity(city);
}
else {
updateMarkerCity('Onbekend');
}
});
}
function updateMarkerCity(str) {
document.getElementById('locality').innerHTML = str;
}
/**
* geocodeResponse is een object vol adres-gegevens.
* We gaan in die gegevens vissen naar een gegeven dat overeen komt met de parameter "type".
*
* bv. type = 'postal_code' =>
* geocodeResponse.address_components[5].types[1] = 'postal_code'
* geocodeResponse.address_components[5].long_name = '8310'
*/
function addresComponent(type, geocodeResponse) {
for(var i=0; i < geocodeResponse.address_components.length; i++) {
for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
if (geocodeResponse.address_components[i].types[j] == type) {
return geocodeResponse.address_components.long_name;
}
}
}
return '';
}
/**
* A distance widget that will display a circle that can be resized and will
* provide the radius in km.
*
* @param {google.maps.Map} map The map to attach to.
*
* @constructor
*/
function DistanceWidget(map) {
this.set('map', map);
//this.set('updateMarkerAddress', updateMarkerAddress_callback);
this.set('position', map.getCenter());
var marker = new google.maps.Marker({
draggable: true,
title: 'Verplaats me naar een zoekgebied in Nederland!',
icon: dartIcon()
});
google.maps.event.addListener(marker, 'dragend', function(e) {
geocodePosition(distanceWidget.position);
});
// Bind the marker map property to the DistanceWidget map property
marker.bindTo('map', this);
// Bind the marker position property to the DistanceWidget position
// property
marker.bindTo('position', this);
// Create a new radius widget
radiusWidget = new RadiusWidget();
// Bind the radiusWidget map to the DistanceWidget map
radiusWidget.bindTo('map', this);
// Bind the radiusWidget center to the DistanceWidget position
radiusWidget.bindTo('center', this, 'position');
// Bind to the radiusWidgets' distance property
this.bindTo('distance', radiusWidget);
// Bind to the radiusWidgets' bounds property
this.bindTo('bounds', radiusWidget);
}
DistanceWidget.prototype = new google.maps.MVCObject();
/**
* A radius widget that add a circle to a map and centers on a marker.
*
* @constructor
*/
function RadiusWidget() {
/**
* nieuwe code
*/
if (updateMarkerAddress('Onbekend')){
hex = '#FF0000';
}
else{
hex = '#007A98';
}
var circle = new google.maps.Circle({
strokeWeight: 2,
strokeColor: hex,
fillOpacity: 0.2
});
// Set the distance property value, default to 50km.
this.set('distance', perimeterDefault);
this.set('perimeterMin', perimeterMin);
this.set('perimeterMax', perimeterMax);
this.set('circle', circle);
// Bind the RadiusWidget bounds property to the circle bounds property.
this.bindTo('bounds', circle);
// Bind the circle center to the RadiusWidget center property
circle.bindTo('center', this);
// Bind the circle map to the RadiusWidget map
circle.bindTo('map', this);
// Bind the circle radius property to the RadiusWidget radius property
circle.bindTo('radius', this);
// Add the sizer marker
this.addSizer_();
}
RadiusWidget.prototype = new google.maps.MVCObject();
/**
* Update the radius when the distance has changed.
*/
RadiusWidget.prototype.distance_changed = function() {
if (this.get('distance') < 5) {
this.set('distance', 5);
}
if (this.get('distance') > 100) {
this.set('distance', 100);
}
this.set('radius', this.get('distance') * 1000);
};
/**
* Add the sizer marker to the map.
*
* @private
*/
RadiusWidget.prototype.addSizer_ = function() {
var sizer = new google.maps.Marker({
draggable: true,
title: 'Sleep mij om het zoekgebied te vergroten of te verkleinen!',
icon: sliderIcon()
});
this.set('slider', sizer);
sizer.bindTo('map', this);
sizer.bindTo('position', this, 'sizer_position');
var me = this;
google.maps.event.addListener(sizer, 'drag', function() {
// Set the circle distance (radius)
me.setDistance();
});
google.maps.event.addListener(sizer, 'dragend', function() {
// Set the circle distance (radius)
me.sliderDropped();
});
};
/**
* Update the center of the circle and position the sizer back on the line.
*
* Position is bound to the DistanceWidget so this is expected to change when
* the position of the distance widget is changed.
*/
RadiusWidget.prototype.center_changed = function() {
var bounds = this.get('bounds');
// Bounds might not always be set so check that it exists first.
if (bounds) {
var lng = bounds.getNorthEast().lng();
// Put the sizer at center, right on the circle.
var position = new google.maps.LatLng(this.get('center').lat(), lng);
this.set('sizer_position', position);
}
};
/**
* Calculates the distance between two latlng points in km.
* @see http://www.movable-type.co.uk/scripts/latlong.html
*
* @param {google.maps.LatLng} p1 The first lat lng point.
* @param {google.maps.LatLng} p2 The second lat lng point.
* @return {number} The distance between the two points in km.
* @private
*/
RadiusWidget.prototype.distanceBetweenPoints_ = function(p1, p2) {
if (!p1 || !p2) {
return 0;
}
var R = 6371; // Radius of the Earth in km
var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
var result = d.toFixed(0);
return result;
};
/**
* Set the distance of the circle based on the position of the sizer.
*/
RadiusWidget.prototype.setDistance = function() {
// As the sizer is being dragged, its position changes. Because the
// RadiusWidget's sizer_position is bound to the sizer's position, it will
// change as well.
var pos = this.get('sizer_position');
var center = this.get('center');
var distance = this.distanceBetweenPoints_(center, pos);
// Set the distance property for any objects that are bound to it
this.set('distance', distance);
};
/**
* The slider is dropped. We will check if the the marker is dropped within the range set by the user/developper
*/
RadiusWidget.prototype.sliderDropped = function() {
var pos = this.get('sizer_position');
var center = this.get('center');
var distance = this.distanceBetweenPoints_(center, pos);
if (distance > this.perimeterMax || distance < this.perimeterMin) {
this.slider.setPosition(
new google.maps.LatLng(
center.lat(), //
this.circle.getBounds().getNorthEast().lng() // far east of circle
)
);
}
};
var distanceWidget;
var radiusWidget;
var map;
var geocoder;
var perimeterMin = 5;
var perimeterMax = 100;
var perimeterDefault = 30;
function sliderIcon() {
return new google.maps.MarkerImage(
'Images/resize-off.png',
// size
new google.maps.Size(16, 16),
// The origin for this image
new google.maps.Point(0,0),
// The anchor for this image
new google.maps.Point(8, 8)
);
}
function dartIcon() {
return new google.maps.MarkerImage(
'Images/icon_dart4.png',
// size
new google.maps.Size(47, 41),
// The origin for this image
new google.maps.Point(0,0),
// The anchor for this image
new google.maps.Point(24,24)
);
}
$(document).ready(function(){
geocoder = new google.maps.Geocoder();
var center = new google.maps.LatLng(52.093271,5.121850);
var zoom = 8;
var mapDiv;
function init() {
mapDiv = document.getElementById('mapcanvas');
map = new google.maps.Map(mapDiv, {
center: center,
zoom: zoom,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
distanceWidget = new DistanceWidget(map, updateMarkerAddress);
google.maps.event.addListener(distanceWidget, 'distance_changed', function(e) {
display();
//$("#controls #perimeter_distance").val(radiusWidget.distance);
});
google.maps.event.addListener(distanceWidget, 'position_changed', function(e) {
//geocodePosition(distanceWidget.position);
display();
//$("#controls #perimeter_center").val(distanceWidget.position);
});
display();
geocodePosition(center);
}
google.maps.event.addDomListener(window, 'load', init);
});
function display() {
$("#controls #perimeter_distance_html").val(radiusWidget.distance);
$("#controls #perimeter_distance_value").html(radiusWidget.distance);
$("#controls #perimeter_center_lat").val(distanceWidget.position.lat().toString() );
$("#controls #perimeter_center_lng").val(distanceWidget.position.lng().toString() );
$("#controls #perimeter_north").val(radiusWidget.circle.getBounds().getNorthEast().lat());
$("#controls #perimeter_east").val(radiusWidget.circle.getBounds().getNorthEast().lng());
$("#controls #perimeter_south").val(radiusWidget.circle.getBounds().getSouthWest().lat());
$("#controls #perimeter_west").val(radiusWidget.circle.getBounds().getSouthWest().lng());
}
function updateMarkerAddress(str) {
$("#controls #perimeter_postal").val(str);
}
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
var city = addresComponent('locality', responses[0])
var postal_code = addresComponent('postal_code', responses[0])
if (postal_code) {
updateMarkerAddress(postal_code);
}
else {
updateMarkerAddress('Onbekend');
}
}
else {
updateMarkerAddress('Onbekend');
}
if (city) {
updateMarkerCity(city);
}
else {
updateMarkerCity('Onbekend');
}
});
}
function updateMarkerCity(str) {
document.getElementById('locality').innerHTML = str;
}
/**
* geocodeResponse is een object vol adres-gegevens.
* We gaan in die gegevens vissen naar een gegeven dat overeen komt met de parameter "type".
*
* bv. type = 'postal_code' =>
* geocodeResponse.address_components[5].types[1] = 'postal_code'
* geocodeResponse.address_components[5].long_name = '8310'
*/
function addresComponent(type, geocodeResponse) {
for(var i=0; i < geocodeResponse.address_components.length; i++) {
for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
if (geocodeResponse.address_components[i].types[j] == type) {
return geocodeResponse.address_components.long_name;
}
}
}
return '';
}
/**
* A distance widget that will display a circle that can be resized and will
* provide the radius in km.
*
* @param {google.maps.Map} map The map to attach to.
*
* @constructor
*/
function DistanceWidget(map) {
this.set('map', map);
//this.set('updateMarkerAddress', updateMarkerAddress_callback);
this.set('position', map.getCenter());
var marker = new google.maps.Marker({
draggable: true,
title: 'Verplaats me naar een zoekgebied in Nederland!',
icon: dartIcon()
});
google.maps.event.addListener(marker, 'dragend', function(e) {
geocodePosition(distanceWidget.position);
});
// Bind the marker map property to the DistanceWidget map property
marker.bindTo('map', this);
// Bind the marker position property to the DistanceWidget position
// property
marker.bindTo('position', this);
// Create a new radius widget
radiusWidget = new RadiusWidget();
// Bind the radiusWidget map to the DistanceWidget map
radiusWidget.bindTo('map', this);
// Bind the radiusWidget center to the DistanceWidget position
radiusWidget.bindTo('center', this, 'position');
// Bind to the radiusWidgets' distance property
this.bindTo('distance', radiusWidget);
// Bind to the radiusWidgets' bounds property
this.bindTo('bounds', radiusWidget);
}
DistanceWidget.prototype = new google.maps.MVCObject();
/**
* A radius widget that add a circle to a map and centers on a marker.
*
* @constructor
*/
function RadiusWidget() {
/**
* nieuwe code
*/
if (updateMarkerAddress('Onbekend')){
hex = '#FF0000';
}
else{
hex = '#007A98';
}
var circle = new google.maps.Circle({
strokeWeight: 2,
strokeColor: hex,
fillOpacity: 0.2
});
// Set the distance property value, default to 50km.
this.set('distance', perimeterDefault);
this.set('perimeterMin', perimeterMin);
this.set('perimeterMax', perimeterMax);
this.set('circle', circle);
// Bind the RadiusWidget bounds property to the circle bounds property.
this.bindTo('bounds', circle);
// Bind the circle center to the RadiusWidget center property
circle.bindTo('center', this);
// Bind the circle map to the RadiusWidget map
circle.bindTo('map', this);
// Bind the circle radius property to the RadiusWidget radius property
circle.bindTo('radius', this);
// Add the sizer marker
this.addSizer_();
}
RadiusWidget.prototype = new google.maps.MVCObject();
/**
* Update the radius when the distance has changed.
*/
RadiusWidget.prototype.distance_changed = function() {
if (this.get('distance') < 5) {
this.set('distance', 5);
}
if (this.get('distance') > 100) {
this.set('distance', 100);
}
this.set('radius', this.get('distance') * 1000);
};
/**
* Add the sizer marker to the map.
*
* @private
*/
RadiusWidget.prototype.addSizer_ = function() {
var sizer = new google.maps.Marker({
draggable: true,
title: 'Sleep mij om het zoekgebied te vergroten of te verkleinen!',
icon: sliderIcon()
});
this.set('slider', sizer);
sizer.bindTo('map', this);
sizer.bindTo('position', this, 'sizer_position');
var me = this;
google.maps.event.addListener(sizer, 'drag', function() {
// Set the circle distance (radius)
me.setDistance();
});
google.maps.event.addListener(sizer, 'dragend', function() {
// Set the circle distance (radius)
me.sliderDropped();
});
};
/**
* Update the center of the circle and position the sizer back on the line.
*
* Position is bound to the DistanceWidget so this is expected to change when
* the position of the distance widget is changed.
*/
RadiusWidget.prototype.center_changed = function() {
var bounds = this.get('bounds');
// Bounds might not always be set so check that it exists first.
if (bounds) {
var lng = bounds.getNorthEast().lng();
// Put the sizer at center, right on the circle.
var position = new google.maps.LatLng(this.get('center').lat(), lng);
this.set('sizer_position', position);
}
};
/**
* Calculates the distance between two latlng points in km.
* @see http://www.movable-type.co.uk/scripts/latlong.html
*
* @param {google.maps.LatLng} p1 The first lat lng point.
* @param {google.maps.LatLng} p2 The second lat lng point.
* @return {number} The distance between the two points in km.
* @private
*/
RadiusWidget.prototype.distanceBetweenPoints_ = function(p1, p2) {
if (!p1 || !p2) {
return 0;
}
var R = 6371; // Radius of the Earth in km
var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
var result = d.toFixed(0);
return result;
};
/**
* Set the distance of the circle based on the position of the sizer.
*/
RadiusWidget.prototype.setDistance = function() {
// As the sizer is being dragged, its position changes. Because the
// RadiusWidget's sizer_position is bound to the sizer's position, it will
// change as well.
var pos = this.get('sizer_position');
var center = this.get('center');
var distance = this.distanceBetweenPoints_(center, pos);
// Set the distance property for any objects that are bound to it
this.set('distance', distance);
};
/**
* The slider is dropped. We will check if the the marker is dropped within the range set by the user/developper
*/
RadiusWidget.prototype.sliderDropped = function() {
var pos = this.get('sizer_position');
var center = this.get('center');
var distance = this.distanceBetweenPoints_(center, pos);
if (distance > this.perimeterMax || distance < this.perimeterMin) {
this.slider.setPosition(
new google.maps.LatLng(
center.lat(), //
this.circle.getBounds().getNorthEast().lng() // far east of circle
)
);
}
};
Dus je geeft 360 regels code en vertelt niet eens wat er mis gaat. Denk je dat iemand even die code voor je gaat doorspitten of zo?
probleem: de variabele "strokeColor van de cirkel blijft op waarde '#007A98' staan.
Ik heb nu zelf een if else functie ingebouwd maar ben (nog) niet bekend met javascript.
het is wat trial & error maar kom er nog niet uit.
Je geeft hier helemaal geen waarde terug. Dat zal dus ook nooit true worden (of niet false beter gezegd).
Een paar puntjes:
- function addresComponent; lijn 137:
anders krijg je nooit een adres (-component) terug.
- updateMarkerAddress zou je op deze manier kunnen gebruiken:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function updateMarkerAddress(str) {
$("#controls #perimeter_postal").val(str);
if (str == 'Onbekend') {
var hex = '#FF0000';
}
else {
var hex = '#007A98';
}
if (typeof radiusWidget != 'undefined' && typeof radiusWidget.circle != 'undefined') {
radiusWidget.circle.setOptions({
strokeColor: hex
});
}
}
$("#controls #perimeter_postal").val(str);
if (str == 'Onbekend') {
var hex = '#FF0000';
}
else {
var hex = '#007A98';
}
if (typeof radiusWidget != 'undefined' && typeof radiusWidget.circle != 'undefined') {
radiusWidget.circle.setOptions({
strokeColor: hex
});
}
}
Erwin H op 02/09/2012 23:22:16:
... Denk je dat iemand even die code voor je gaat doorspitten of zo?
Het grootste deel van die code heb ik ooit zelf geschreven; dat helpt
Gewijzigd op 03/09/2012 12:15:36 door Kris Peeters
Wat bedoel je eigenlijk met je eerste punt?
return geocodeResponse.address_components.long_name;
Deze staat er toch al?
... Die zorgt trouwens voor BBCode, waardoor je die niet ziet in jouw post hier boven
Gewijzigd op 04/09/2012 11:17:53 door Kris Peeters