質問編集履歴

2

タイトル変更

2017/11/04 13:17

投稿

suraisu
suraisu

スコア6

test CHANGED
@@ -1 +1 @@
1
- 現在地から目的地を一覧から距離計測、option value値の取得
1
+ 現在地から目的地を一覧から距離計測、位置情報で取得した値を初期位置に設定する。
test CHANGED
File without changes

1

誤字、カッコの対応の修正⇒posが定義されない

2017/11/04 13:17

投稿

suraisu
suraisu

スコア6

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ```
12
12
 
13
- Uncaught SyntaxError: Unexpected identifier
13
+ pos is not defined
14
14
 
15
15
  ```
16
16
 
@@ -236,49 +236,7 @@
236
236
 
237
237
  <option value="柏市松葉町五丁目3番地">柏市立松葉第一小学校</option>
238
238
 
239
- <option value="柏市松葉町二丁目16番地">柏市立松葉第二小学校</option>
239
+ <option value="柏市松葉町二丁目16番地">柏市立松葉第二小学校
240
-
241
- <option value="柏市松葉町三丁目14番地">柏市立松葉中学校</option>
242
-
243
- <option value="柏市松葉町四丁目6番">松葉第一近隣公園</option>
244
-
245
- <option value="柏市松葉町四丁目13番">松葉第二近隣公園</option>
246
-
247
- <option value="柏市松ヶ崎1182番地の9">柏市立柏第四小学校</option>
248
-
249
- <option value="柏市高田376番地の3">柏市立高田小学校</option>
250
-
251
- <option value="柏市高田919番地の1">柏市立柏第五中学校</option>
252
-
253
- <option value="柏市松ヶ崎884番地の1">千葉県立柏中央高等学校</option>
254
-
255
- <option value="柏市大山台1番">松ヶ崎中央公園</option>
256
-
257
- <option value="柏市豊四季台四丁目2番1号">柏市立柏第六小学校</option>
258
-
259
- <option value="柏市篠籠田723番地の1">柏市立柏第七小学校</option>
260
-
261
- <option value="柏市明原四丁目1番1号">柏市立柏中学校</option>
262
-
263
- <option value="柏市篠籠田987番地の1">柏市立柏第三中学校</option>
264
-
265
- <option value="柏市明原三丁目1番">西口第一公園</option>
266
-
267
- <option value="柏市旭町六丁目5番17号">柏市立旭小学校</option>
268
-
269
- <option value="柏市旭町五丁目3番9号">柏市立旭東小学校</option>
270
-
271
- <option value="柏市旭町三丁目2番1号">千葉県立東葛飾高等学校</option>
272
-
273
- <option value="柏市旭町7丁目4番81号">気象大学校</option>
274
-
275
- <option value="柏市豊四季310番地">柏市立柏第二小学校</option>
276
-
277
- <option value="柏市豊四季287番地の7">柏市立豊四季中学校</option>
278
-
279
- <option value="柏市豊四季224番地の3">豊四季庚塚緑地</option>
280
-
281
- <option value="柏市あけぼの一丁目7番6号">柏市立柏第一小学校</option>
282
240
 
283
241
 
284
242
 
@@ -302,213 +260,245 @@
302
260
 
303
261
  <script>
304
262
 
305
- //追加
306
-
307
263
  function initMap() {
308
264
 
309
- var directionsService = new google.maps.DirectionsService;
310
-
311
- var directionsDisplay = new google.maps.DirectionsRenderer;
312
-
313
- /*var map = new google.maps.Map(document.getElementById('map'), {
314
-
315
- zoom: 10,
316
-
317
- center: {lat: 35.681, lng: 140.02} //初期
318
-
319
- });*/
320
-
321
- var map = new google.maps.Map(document.getElementById('map'), {
322
-
323
- center: {lat: 35.891820616597954, lng: 139.94462999999996},
324
-
325
- zoom: 16,
326
-
327
- });
328
-
329
- var infoWindow = new google.maps.InfoWindow({map: map});
330
-
331
- // 位置情報検索.
332
-
333
- if (navigator.geolocation) {
334
-
335
- navigator.geolocation.getCurrentPosition(function(position) {
336
-
337
- var pos = {
338
-
339
- lat: position.coords.latitude,
340
-
341
- lng: position.coords.longitude
342
-
343
- };
344
-
345
- infoWindow.setPosition(pos);
346
-
347
- infoWindow.setContent('現在地です');
348
-
349
- map.setCenter(pos);
350
-
351
- }, function() {
352
-
353
- handleLocationError(true, infoWindow, map.getCenter());
354
-
355
- });
356
-
357
- } else {
358
-
359
- // Browser doesn't support Geolocation
360
-
361
- handleLocationError(false, infoWindow, map.getCenter());
362
-
363
- }
364
-
365
- function handleLocationError(browserHasGeolocation, infoWindow, pos) {
366
-
367
- infoWindow.setPosition(pos);
368
-
369
- infoWindow.setContent(browserHasGeolocation ?
370
-
371
- 'Error: The Geolocation service failed.' :
372
-
373
- 'Error: Your browser doesn\'t support geolocation.');
374
-
375
- }
376
-
377
-
378
-
379
- //元のほう追加
380
-
381
- var directionsService = new google.maps.DirectionsService;
382
-
383
- var directionsDisplay = new google.maps.DirectionsRenderer({
384
-
385
- draggable: false,
386
-
387
- // map: map,
388
-
389
- // panel: document.getElementById('right-panel')
390
-
391
- });
392
-
393
- directionsService.route({
394
-
395
- otrigin: pos
396
-
397
- destination: document.getElementById('end').value,
398
-
399
- waypoints: waypts,
400
-
401
- optimizeWaypoints: true,
402
-
403
- travelMode: 'WALKING'
404
-
405
- },
406
-
407
- }
408
-
409
- directionsDisplay.addListener('directions_changed', function() {
410
-
411
- computeTotalDistance(directionsDisplay.getDirections());
412
-
413
- });//ここまで
414
-
415
-
416
-
417
- directionsDisplay.setMap(map);
418
-
419
-
420
-
421
- document.getElementById('submit').addEventListener('click', function() {
422
-
423
- calculateAndDisplayRoute(directionsService, directionsDisplay);
424
-
425
- });//送信ボタンのJS
426
-
427
- function(response, status) {
428
-
429
- if (status === 'OK') {
430
-
431
- directionsDisplay.setDirections(response);
432
-
433
- var route = response.routes[0];
434
-
435
- var summaryPanel = document.getElementById('directions-panel');
436
-
437
- summaryPanel.innerHTML = '';
438
-
439
- // For each route, display summary information.
440
-
441
- for (var d = 0; d < route.legs.length; d++) {
442
-
443
- var routeSegment = d + 1;
444
-
445
- summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
446
-
447
- '</b><br>';
448
-
449
- summaryPanel.innerHTML += route.legs[d].start_address + ' to ';
450
-
451
- summaryPanel.innerHTML += route.legs[d].end_address + '<br>';
452
-
453
- summaryPanel.innerHTML += route.legs[d].distance.text + '<br><br>';
265
+ var directionsService = new google.maps.DirectionsService;
266
+
267
+ var directionsDisplay = new google.maps.DirectionsRenderer;
268
+
269
+ /*var map = new google.maps.Map(document.getElementById('map'), {
270
+
271
+ zoom: 10,
272
+
273
+ center: {lat: 35.681, lng: 140.02} //初期
274
+
275
+ });*/
276
+
277
+ var map = new google.maps.Map(document.getElementById('map'), {
278
+
279
+ center: {lat: 35.891820616597954, lng: 139.94462999999996},
280
+
281
+ zoom: 16,
282
+
283
+ });
284
+
285
+ var infoWindow = new google.maps.InfoWindow({map: map});
286
+
287
+
288
+
289
+ // 位置情報検索.
290
+
291
+ if (navigator.geolocation) {
292
+
293
+ navigator.geolocation.getCurrentPosition(function(position) {
294
+
295
+ var pos = {
296
+
297
+ lat: position.coords.latitude,
298
+
299
+ lng: position.coords.longitude
300
+
301
+ };
302
+
303
+ //infoWindow.setPosition(pos);
304
+
305
+ //infoWindow.setContent('現在地です');
306
+
307
+ map.setCenter(pos);
308
+
309
+ var marker = new google.maps.Marker({
310
+
311
+ position: pos,
312
+
313
+ map: map,
314
+
315
+ title: '現在地です'
316
+
317
+ });
318
+
319
+ }, function() {
320
+
321
+ handleLocationError(true, infoWindow, map.getCenter());
322
+
323
+ });
324
+
325
+ } else {
326
+
327
+ // Browser doesn't support Geolocation
328
+
329
+ handleLocationError(false, infoWindow, map.getCenter());
330
+
331
+ }
332
+
333
+ function handleLocationError(browserHasGeolocation, infoWindow, pos) {
334
+
335
+ infoWindow.setPosition(pos);
336
+
337
+ infoWindow.setContent(browserHasGeolocation ?
338
+
339
+ 'Error: The Geolocation service failed.' :
340
+
341
+ 'Error: Your browser doesn\'t support geolocation.')
342
+
343
+ }
344
+
345
+ //元のほう追加
346
+
347
+ var directionsService = new google.maps.DirectionsService;
348
+
349
+ var directionsDisplay = new google.maps.DirectionsRenderer({
350
+
351
+ draggable: true,
352
+
353
+ // map: map,
354
+
355
+ // panel: document.getElementById('right-panel')
356
+
357
+ });
358
+
359
+ directionsDisplay.addListener('directions_changed', function() {
360
+
361
+ computeTotalDistance(directionsDisplay.getDirections());
362
+
363
+ });//ここまで
364
+
365
+
366
+
367
+
368
+
369
+ directionsDisplay.setMap(map);
370
+
371
+
372
+
373
+
374
+
375
+ document.getElementById('submit').addEventListener('click', function() {
376
+
377
+ calculateAndDisplayRoute(directionsService, directionsDisplay);
378
+
379
+ });//送信ボタンのJS
454
380
 
455
381
  }
456
382
 
457
- } else {
458
-
459
- window.alert('Directions request failed due to ' + status);
460
-
461
- }
462
-
463
- });//追加
464
-
465
- }
466
-
467
-
468
-
469
-
470
-
471
- function computeTotalDistance(result) {
472
-
473
- var total = 0;
474
-
475
- // var time = distance/speed;//追加
476
-
477
- var time = 0;
478
-
479
- var myroute = result.routes[0];
480
-
481
- for (var i = 0; i < myroute.legs.length; i++) {
482
-
483
- total += myroute.legs[i].distance.value;
484
-
485
- }
486
-
487
-
488
-
489
- total = total / 1000;
490
-
491
- time = total / 4.8;
492
-
493
- time = time * 60;
494
-
495
- time = Math.round(time);
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
- document.getElementById('total').innerHTML = total + ' km';
504
-
505
- document.getElementById('time').innerHTML = time + ' 分';
506
-
507
-
508
-
509
- }
510
-
511
-
383
+ function calculateAndDisplayRoute(directionsService, directionsDisplay) {
384
+
385
+ var waypts = [];
386
+
387
+ var checkboxArray = document.getElementById('waypoints');
388
+
389
+ for (var d = 0; d < checkboxArray.length; d++) {
390
+
391
+ if (checkboxArray.options[d].selected) {
392
+
393
+ waypts.push({
394
+
395
+ location: checkboxArray[d].value,
396
+
397
+ stopover: true
398
+
399
+ });
400
+
401
+ }
402
+
403
+ }
404
+
405
+ directionsService.route({
406
+
407
+ origin: pos,
408
+
409
+ destination: document.getElementById('end').value,
410
+
411
+ waypoints: waypts,
412
+
413
+ optimizeWaypoints: true,
414
+
415
+ travelMode: 'WALKING'
416
+
417
+ }, function(response, status) {
418
+
419
+ if (status === 'OK') {
420
+
421
+ directionsDisplay.setDirections(response);
422
+
423
+ var route = response.routes[0];
424
+
425
+ var summaryPanel = document.getElementById('directions-panel');
426
+
427
+ summaryPanel.innerHTML = '';
428
+
429
+ // For each route, display summary information.
430
+
431
+ for (var d = 0; d < route.legs.length; d++) {
432
+
433
+ var routeSegment = d + 1;
434
+
435
+ summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
436
+
437
+ '</b><br>';
438
+
439
+ summaryPanel.innerHTML += route.legs[d].start_address + ' to ';
440
+
441
+ summaryPanel.innerHTML += route.legs[d].end_address + '<br>';
442
+
443
+ summaryPanel.innerHTML += route.legs[d].distance.text + '<br><br>';
444
+
445
+ }
446
+
447
+ } else {
448
+
449
+ window.alert('Directions request failed due to ' + status);
450
+
451
+ }
452
+
453
+ });
454
+
455
+ }
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+ function computeTotalDistance(result) {
464
+
465
+ var total = 0;
466
+
467
+ // var time = distance/speed;//追加
468
+
469
+ var time = 0;
470
+
471
+ var myroute = result.routes[0];
472
+
473
+ for (var i = 0; i < myroute.legs.length; i++) {
474
+
475
+ total += myroute.legs[i].distance.value;
476
+
477
+ }
478
+
479
+
480
+
481
+ total = total / 1000;
482
+
483
+ time = total / 4.8;
484
+
485
+ time = time * 60;
486
+
487
+ time = Math.round(time);
488
+
489
+
490
+
491
+
492
+
493
+
494
+
495
+ document.getElementById('total').innerHTML = total + ' km';
496
+
497
+ document.getElementById('time').innerHTML = time + ' 分';
498
+
499
+
500
+
501
+ }
512
502
 
513
503
 
514
504
 
@@ -638,11 +628,9 @@
638
628
 
639
629
  ###試したこと
640
630
 
631
+ 位置情報検索でposを初期位置にするのですが、うまく定義できません。
632
+
641
- 現在地を変数にしたposを初期位置にするためにfunction initmap全部に囲ったのですが、目的地を指定するところでエラーになってしまいます。
633
+ posを初期位置にするために変数範囲広くしりしたのですがどうもうせん…
642
-
643
- 試しにorigin値をdocument.getElementById('end').value,にすると通ってしまい、次のエラーに行きつきます。
644
-
645
- これはどういう事なのでしょうか…
646
634
 
647
635
 
648
636