teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

キーが載ってしまっていた

2022/08/17 08:23

投稿

tuki43
tuki43

スコア12

title CHANGED
File without changes
body CHANGED
@@ -64,7 +64,7 @@
64
64
 
65
65
  var userMap = new google.maps.LatLng(latitude, longitude);
66
66
 
67
- var apiKey = 'AIzaSyAh3uPR32DOP6SML_dqgClMASTZIIMC1Zg';
67
+ var apiKey = 'あああ';
68
68
 
69
69
  var opts = {
70
70
  zoom: 11,

5

誤字

2022/08/17 08:20

投稿

tuki43
tuki43

スコア12

title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,7 @@
15
15
 
16
16
  ー----------------
17
17
  2022/08/17更新
18
- 子画面に出す方法をやめてみました。ころが画面遷移でも同じく現在位置を求めてきませんでした。
18
+ 子画面に出す方法をやめてみました。ころが画面遷移でも同じく現在位置を求めてきませんでした。
19
19
  あらためまして、jsファイルの中身全部コピーして貼り付けます。
20
20
 
21
21
  ### 実現したいこと

4

子画面に表示ではなく、画面遷移に変更しました。

2022/08/17 08:20

投稿

tuki43
tuki43

スコア12

title CHANGED
File without changes
body CHANGED
@@ -13,6 +13,11 @@
13
13
  何か対処法はありますでしょうか?
14
14
  よろしくお願いします。
15
15
 
16
+ ー----------------
17
+ 2022/08/17更新
18
+ 子画面に出す方法をやめてみました。どころが画面遷移でも同じく現在位置を求めてきませんでした。
19
+ あらためまして、jsファイルの中身全部コピーして貼り付けます。
20
+
16
21
  ### 実現したいこと
17
22
 
18
23
  WordPressの親画面にボタンを設置し、ボタンをクリックしたらモーダルウィンドウを表示しそのモーダルウィンドウ内に地図を表示させたい。
@@ -32,184 +37,237 @@
32
37
 
33
38
  ### 該当のソースコード
34
39
 
35
- ```JavaScript
40
+ ```JSファイル
36
- // 確認画面モーダル
37
- $(function () {
38
-   // 開くボタンをクリックしたらモーダルを表示する
39
-   open.on('click', function () {
41
+ if (location.pathname === '/satei/satei_cycle'){
40
-    // GPS位置情報を取得する
42
+ //ユーザーの現在の位置情報を取得
41
- navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
43
+ navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
42
-
43
- // ↑で失敗するためここから先は未実装
44
44
 
45
- return false;
46
- // 取得に成功した場合の処理
45
+ /***** ユーザーの現在の位置情報を取得 *****/
47
- function successCallback(position){
46
+ function successCallback(position) {
47
+
48
+ var requestAjax = function(endpoint, callback) {
49
+ var xhr = new XMLHttpRequest();
50
+ xhr.onreadystatechange = function(){
51
+ if (this.readyState==4 && this.status==200) {
52
+ callback(this.response);
53
+ }
54
+ };
55
+ xhr.responseType = 'json';
56
+ xhr.open('GET',endpoint,true);
57
+ xhr.send();
58
+ };
59
+
48
- // 緯度を取得し画面に表示
60
+ // 現在地の緯度
49
61
  var latitude = position.coords.latitude;
50
- document.getElementById("ido").innerHTML = latitude;
51
- // 経度を取得し画面に表示
62
+ // 現在地の経度
52
- var longitude = position.coords.longitude;
63
+ var longitude = position.coords.longitude;
53
- document.getElementById("keido").innerHTML = longitude;
54
- };
55
- // 取得に失敗した場合の処理
56
- function errorCallback(error){
57
- var err_msg = "";
58
- switch(error.code)
59
- {
60
- case 1:
61
- err_msg = "位置情報の利用が許可されていません";
62
- break;
63
- case 2:
64
- err_msg = "デバイスの位置が判定できません";
65
- break;
66
- case 3:
67
- err_msg = "タイムアウトしました";
68
- break;
69
- }
70
- alert(err_msg)
71
-
72
- };
73
64
 
74
- });
65
+ var userMap = new google.maps.LatLng(latitude, longitude);
75
66
 
67
+ var apiKey = 'AIzaSyAh3uPR32DOP6SML_dqgClMASTZIIMC1Zg';
76
68
 
69
+ var opts = {
70
+ zoom: 11,
71
+ center: new google.maps.LatLng(latitude, longitude)
72
+ };
77
73
 
74
+ var requestURL = 'https://maps.googleapis.com/maps/api/geocode/json?language=ja&sensor=false';
75
+ requestURL += '&latlng=' + latitude + ',' + longitude;
76
+ requestURL += '&key=' + apiKey;
78
77
 
78
+ // 現在地の緯度経度
79
+ var origin1 = {lat: latitude, lng: longitude};
80
+
81
+ // Geocoderのオブジェクト
82
+ var geocoder = new google.maps.Geocoder;
79
83
 
80
- ```
81
- ```PHP
82
- <!-- モーダル本体 -->
84
+ // DistanceMatrixServiceのオブジェクト
83
- <div class="modal-container">
85
+ var service = new google.maps.DistanceMatrixService;
84
- <div class="modal-body">
85
- <!-- 閉じるボタン -->
86
- <div class="modal-close">X</div>
87
- <!-- モーダル内のコンテンツ -->
88
- <div class="modal-content">
89
- <p>現在地から探す</p>
90
-       <!-- ↓ここに地図を表示させる予定 -->
91
- <div id="map2" style="width:620px; height:400px"></div>
92
- <div id="show_result"></div>
93
- <label class="col-sm-2 modal-label">緯度</label>
94
- <p class="modal_ido modal-value"><span id="ido">???</span><span>度</span></p>
95
- <label class="col-sm-2 modal-label">経度</label>
96
- <p class="modal_keido modal-value"><span id="keido">???</span><span>度</span></p>
97
-
98
- <div>
99
- <button type="submit" name="select-shop" class="reset flat border" value="選択">
100
- 選択
101
- </button>
102
- </div>
103
- <script async defer
104
- src="https://maps.googleapis.com/maps/api/js?key=APIキー&callback=initMap">
105
- </script>
106
- </div>
107
- </div>
108
- </div>
109
- ```
110
- ```css
111
- /*モーダル内で表示するラベル*/
112
- .modal-label {
113
- text-align: left;
114
- font-weight: bold;
115
- }
116
86
 
117
- /*モーダル内で表示する値*/
87
+ // 店舗の経度緯度の配列(この配列と下記の連想配列の順番は同じにする
118
- .modal-value {
119
- margin-left: 30px;
88
+ var org = new Array();
89
+ org.push(new google.maps.LatLng(35.xxxx, 139.xxxxx)); //〇〇店
90
+ org.push(new google.maps.LatLng(35.xxxx, 139.xxxxx)); //〇〇店
91
+ org.push(new google.maps.LatLng(35.xxxx, 139.xxxx)); //〇〇店
92
+
93
+ // 連想配列 店舗名と住所、3つ目に目的地までの距離を最終的に追加
94
+ var shopList = [
95
+ {name:'〇〇店', add:'神奈川県xxxxx', lat:35.xxxx, lng:139.xxxx },
96
+ {name:'〇〇店', add:'神奈川県xxxxxx', lat:35.xxxx, lng:139.xxxxx},
97
+ {name:'〇〇店', add:'神奈川県xxxx, lat:35.xxx, lng: 139.xxxx},
98
+ {name:'〇〇店', add:'東京都xxxx', lat:35.xxxx, lng: 139.xxx},
99
+ {name:'〇〇店', add:'東京都xxxx', lat:35.xxxx, lng: 139.xxxx},
100
+ {name:'〇〇店', add:'東京都xxxx', lat:35.xxx, lng: 139.xxx}
120
- }
101
+ ];
121
102
 
122
- /*モーダルを開くボタン*/
103
+ // DistanceMatrixの実行
104
+ service.getDistanceMatrix({
123
- .modal-open {
105
+ origins: [origin1],
106
+ // 出発地のLatLngオブジェクトの配列
124
- position: fixed;
107
+ destinations: org,
108
+ //目的地のLatLngオブジェクトの配列
109
+ travelMode: 'DRIVING',
110
+ //DRIVING=自動車,BICYCLING=自転車,TRANSIT=電車,WALKING=徒歩
111
+ unitSystem: google.maps.UnitSystem.METRIC,
112
+ //距離を表示する際に使用される優先単位系
113
+ //METRIC=の距離をメートル法の単位で表す。
125
- display: flex;
114
+ avoidHighways: false,
115
+ //true の場合、可能な限り高速道路を避ける(省略可)
126
- align-items: center;
116
+ avoidTolls: false
127
- justify-content: center;
128
- top: 50%;
129
- left: 50%;
130
- width: 300px;
131
- height: 50px;
132
- font-weight: bold;
133
- color: #fff;
134
- background: #000;
135
- margin: auto;
136
- cursor: pointer;
137
- transform: translate(-50%, -50%);
117
+ //true の場合、可能な限り有料道路を避ける(省略可
138
- }
118
+ },
119
+ function(response, status) {
120
+ if (status !== 'OK') {
121
+ alert('Error was: ' + status);
122
+ } else {
123
+ var originList = response.originAddresses;
124
+ var outputDiv = document.getElementById('output');
125
+ outputDiv.innerHTML = '';
139
126
 
140
- /*モーダル本体の指定 + モーダル外側の背景の指定*/
127
+ for (var i = 0; i < originList.length; i++) {
141
- .modal-container {
142
- position: fixed;
143
- top: 0;
144
- left: 0;
145
- width: 100%;
146
- height: 100%;
147
- text-align: center;
148
- background: rgba(0, 0, 0, 50%);
128
+ var results = response.rows[i].elements;
149
- padding: 40px 20px;
150
- overflow: auto;
151
- opacity: 0;
152
- visibility: hidden;
153
- transition: .3s;
154
- box-sizing: border-box;
155
- }
156
129
 
157
- /*モーダル本体の擬似要素の指定*/
130
+ // Geocoderの呼び出し
158
- .modal-container:before {
131
+ geocoder.geocode
159
- content: "";
160
- display: inline-block;
161
- vertical-align: middle;
162
- height: 100%;
163
- }
164
132
 
133
+ for (var j = 0; j < results.length; j++) {
165
- /*モダル本体に「active」クラス付与した時スタイル*/
134
+ // xxxxx 数値 フォマットされていないも
135
+ var distance = results[j].distance.value;
136
+ // xx.x km フォーマットされたもの
166
- .modal-container.active {
137
+ var kyori = results[j].distance.text;
167
- opacity: 1;
168
- visibility: visible;
138
+ // 距離(単位メートル)
169
- }
139
+ outputDiv.innerHTML += results[j].distance.text;
170
140
 
171
- /*モーダル枠の指定*/
172
- .modal-body {
173
- position: relative;
141
+ shopList[j].code = distance;
174
- display: inline-block;
175
- vertical-align: middle;
142
+ var n = shopList[j].code;
176
- max-width: 500px;
177
- width: 90%;
178
- }
179
143
 
180
- /*モーダルを閉じるボタンの指定*/
144
+
181
- .modal-close {
182
- position: absolute;
183
- display: flex;
184
- align-items: center;
185
- justify-content: center;
186
- top: -40px;
187
- right: -40px;
188
- width: 40px;
189
- height: 40px;
190
- font-size: 40px;
191
- color: #fff;
192
- cursor: pointer;
193
- }
145
+ }
146
+ }
194
147
 
195
- /*モーダル内のコンテンツの指定*/
148
+ // 繰り返し処理終わった後で、shopList 距離順にソートかける
196
- .modal-content {
149
+ shopList.sort((a, b)=> {
197
- background: #fff;
150
+ if(a.code < b.code) return -1;
198
- text-align: left;
151
+ else if(a.code > b.code) return 1;
199
- padding: 30px;
152
+ return 0;
200
- }
153
+ })
154
+ console.log(shopList);
201
155
 
202
- /*モーダル時のスクロール固定*/
203
- body.fixed {
204
- position: fixed !important;
156
+ var map = new google.maps.Map(document.getElementById("map2"), opts);
205
- left: 0;
206
- }
207
157
 
158
+ // 2番目までの店舗にピンを立てたい
159
+ // 1番近い店舗
160
+ var m_latlng1 = new google.maps.LatLng(shopList[0].lat, shopList[0].lng);
161
+ var marker1 = new google.maps.Marker({
162
+ position: m_latlng1,
163
+ map: map,
164
+ title: shopList[0].name,
165
+ icon:{
166
+ url: 'pin.png'
167
+ }
168
+
169
+ });
170
+
171
+ // 2番目に近い店舗
172
+ var m_latlng2 = new google.maps.LatLng(shopList[1].lat, shopList[1].lng);
173
+ var marker2 = new google.maps.Marker({
174
+ position: m_latlng2,
175
+ map: map,
176
+ title: shopList[1].name,
177
+ icon:{
178
+ url: 'pin.png'
179
+ }
180
+ });
181
+
182
+ // 3番目に近い店舗
183
+ var m_latlng3 = new google.maps.LatLng(shopList[2].lat, shopList[2].lng);
184
+ var marker3 = new google.maps.Marker({
185
+ position: m_latlng3,
186
+ map: map,
187
+ title: shopList[2].name,
188
+ icon:{
189
+ url: 'pin.png'
190
+ }
191
+ });
192
+
193
+ // ユーザーの自宅にもピンを立てる
194
+ var marker4 = new google.maps.Marker({
195
+ position: userMap,
196
+ map: map,
197
+ title: '現在地',
198
+ icon:{
199
+ url: 'user-pin.png'
200
+ }
201
+ });
202
+
203
+ // 並び替えが終わったshopListを元の画面に返す
204
+ }
205
+ });
206
+
207
+
208
+ requestAjax(requestURL, function(response){
209
+
210
+ if (response.error_message) {
211
+ alert("住所を取得することが出来ませんでした");
212
+ console.log(response.error_message);
213
+ } else {
214
+ var formattedAddress = response.results[0]['formatted_address'];
215
+ // 住所は「日本、〒100-0005 東京都千代田区丸の内一丁目」の形式
216
+ var data = formattedAddress.split(' ');
217
+ if (data[1]) {
218
+ // id=addressに住所を設定する
219
+ document.getElementById('address').innerHTML = data[1];
220
+ }
221
+ }
222
+ });
223
+ }
224
+
225
+ /***** 位置情報が取得できない場合 *****/
226
+ function errorCallback(error) {
227
+ var err_msg = "";
228
+ switch(error.code)
229
+ {
230
+ case 1:
231
+ err_msg = "位置情報の利用が許可されていません";
232
+ break;
233
+ case 2:
234
+ err_msg = "デバイスの位置が判定できません";
235
+ break;
236
+ case 3:
237
+ err_msg = "タイムアウトしました";
238
+ break;
239
+ }
240
+ alert(err_msg);
241
+ }
242
+ };
243
+
244
+
245
+
246
+
208
247
  ```
248
+ ```PHP
249
+ functions.php
209
250
 
251
+ <?php
252
+ if ($isSelectGpsOver) {
253
+ ?>
254
+ <form name="select-shop-gps" method="post" action="/satei/satei_cycle">
255
+ <div class="panel-heading">
256
+ <h2 class="obi">現住所から選択</h2>
257
+ </div>
258
+ <div id="map2" style="width:620px; height:400px"></div>
259
+ <script async defer
260
+ src="https://maps.googleapis.com/maps/api/js?key=APIキーcallback=initMap">
261
+ </script>
262
+ </form>
263
+ <?php
264
+ }
265
+ ?>
266
+ ```
267
+
210
268
  ### 試したこと
211
269
 
212
- ここに問題に対して試したこと記載してくだい。
270
+ GPSから店舗検索するボタンを押れたら画面遷移するように修正
213
271
 
214
272
  ### 補足情報(FW/ツールのバージョンなど)
215
273
 

3

まったくの初心者です。

2022/08/15 07:48

投稿

tuki43
tuki43

スコア12

title CHANGED
@@ -1,1 +1,1 @@
1
- JavaScript Geolocation モーダルウィンドウでAPIの利用が許可され
1
+ JavaScript Geolocation モーダルウィンドウでAPIの利用が許可されず困ってます
body CHANGED
File without changes

2

HTML,CSSを追加いたしました。div要素でモーダルウィンドウを実現しています。

2022/08/15 06:50

投稿

tuki43
tuki43

スコア12

title CHANGED
File without changes
body CHANGED
@@ -78,11 +78,135 @@
78
78
 
79
79
 
80
80
  ```
81
- ```php
81
+ ```PHP
82
+ <!-- モーダル本体 -->
83
+ <div class="modal-container">
84
+ <div class="modal-body">
85
+ <!-- 閉じるボタン -->
86
+ <div class="modal-close">X</div>
87
+ <!-- モーダル内のコンテンツ -->
88
+ <div class="modal-content">
89
+ <p>現在地から探す</p>
90
+       <!-- ↓ここに地図を表示させる予定 -->
91
+ <div id="map2" style="width:620px; height:400px"></div>
92
+ <div id="show_result"></div>
93
+ <label class="col-sm-2 modal-label">緯度</label>
94
+ <p class="modal_ido modal-value"><span id="ido">???</span><span>度</span></p>
95
+ <label class="col-sm-2 modal-label">経度</label>
96
+ <p class="modal_keido modal-value"><span id="keido">???</span><span>度</span></p>
97
+
98
+ <div>
99
+ <button type="submit" name="select-shop" class="reset flat border" value="選択">
100
+ 選択
101
+ </button>
102
+ </div>
103
+ <script async defer
82
- <script async defer src="https://maps.googleapis.com/maps/api/js?key=APIキー&callback=initMap">
104
+ src="https://maps.googleapis.com/maps/api/js?key=APIキー&callback=initMap">
83
105
  </script>
106
+ </div>
107
+ </div>
108
+ </div>
84
109
  ```
110
+ ```css
111
+ /*モーダル内で表示するラベル*/
112
+ .modal-label {
113
+ text-align: left;
114
+ font-weight: bold;
115
+ }
85
116
 
117
+ /*モーダル内で表示する値*/
118
+ .modal-value {
119
+ margin-left: 30px;
120
+ }
121
+
122
+ /*モーダルを開くボタン*/
123
+ .modal-open {
124
+ position: fixed;
125
+ display: flex;
126
+ align-items: center;
127
+ justify-content: center;
128
+ top: 50%;
129
+ left: 50%;
130
+ width: 300px;
131
+ height: 50px;
132
+ font-weight: bold;
133
+ color: #fff;
134
+ background: #000;
135
+ margin: auto;
136
+ cursor: pointer;
137
+ transform: translate(-50%, -50%);
138
+ }
139
+
140
+ /*モーダル本体の指定 + モーダル外側の背景の指定*/
141
+ .modal-container {
142
+ position: fixed;
143
+ top: 0;
144
+ left: 0;
145
+ width: 100%;
146
+ height: 100%;
147
+ text-align: center;
148
+ background: rgba(0, 0, 0, 50%);
149
+ padding: 40px 20px;
150
+ overflow: auto;
151
+ opacity: 0;
152
+ visibility: hidden;
153
+ transition: .3s;
154
+ box-sizing: border-box;
155
+ }
156
+
157
+ /*モーダル本体の擬似要素の指定*/
158
+ .modal-container:before {
159
+ content: "";
160
+ display: inline-block;
161
+ vertical-align: middle;
162
+ height: 100%;
163
+ }
164
+
165
+ /*モーダル本体に「active」クラス付与した時のスタイル*/
166
+ .modal-container.active {
167
+ opacity: 1;
168
+ visibility: visible;
169
+ }
170
+
171
+ /*モーダル枠の指定*/
172
+ .modal-body {
173
+ position: relative;
174
+ display: inline-block;
175
+ vertical-align: middle;
176
+ max-width: 500px;
177
+ width: 90%;
178
+ }
179
+
180
+ /*モーダルを閉じるボタンの指定*/
181
+ .modal-close {
182
+ position: absolute;
183
+ display: flex;
184
+ align-items: center;
185
+ justify-content: center;
186
+ top: -40px;
187
+ right: -40px;
188
+ width: 40px;
189
+ height: 40px;
190
+ font-size: 40px;
191
+ color: #fff;
192
+ cursor: pointer;
193
+ }
194
+
195
+ /*モーダル内のコンテンツの指定*/
196
+ .modal-content {
197
+ background: #fff;
198
+ text-align: left;
199
+ padding: 30px;
200
+ }
201
+
202
+ /*モーダル時のスクロール固定*/
203
+ body.fixed {
204
+ position: fixed !important;
205
+ left: 0;
206
+ }
207
+
208
+ ```
209
+
86
210
  ### 試したこと
87
211
 
88
212
  ここに問題に対して試したことを記載してください。

1

### 実現したいことの追加をしました。モーダルウィンドウで実現したい。またはそのほかの方法があればお願いします。

2022/08/14 12:56

投稿

tuki43
tuki43

スコア12

title CHANGED
File without changes
body CHANGED
@@ -22,6 +22,7 @@
22
22
  - [ ] ブラウザを新規にオープンさせて地図を表示させたくない
23
23
  - [ ] モーダルウィンドウに地図を表示させて、現在地から近い店舗を表示
24
24
  - [ ] 店舗を「選択」を押したらモーダルウィンドウを閉じる
25
+ - [ ] 選択された店舗の店舗名や住所を親画面に反映
25
26
 
26
27
  ### 発生している問題・エラーメッセージ
27
28