質問編集履歴

1

追記

2020/08/21 09:23

投稿

ringoame49
ringoame49

スコア46

test CHANGED
File without changes
test CHANGED
@@ -149,3 +149,119 @@
149
149
  以上、なぜグローバル変数が表示されないのかとvar address = "";の記述が必要なのかをご教授いただけますと幸いです。
150
150
 
151
151
  よろしくお願いいたします。
152
+
153
+
154
+
155
+ 追記
156
+
157
+ 現在のコード
158
+
159
+ ```
160
+
161
+ <body>
162
+
163
+ <div id="result"></div>
164
+
165
+ <div id="result2"></div>
166
+
167
+ </body>
168
+
169
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
170
+
171
+ <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXXXXX"></script>
172
+
173
+ <script type="text/javascript">
174
+
175
+
176
+
177
+ if( navigator.geolocation ){
178
+
179
+
180
+
181
+ navigator.geolocation.getCurrentPosition(
182
+
183
+
184
+
185
+ function( position ){
186
+
187
+
188
+
189
+ var data = position.coords;
190
+
191
+
192
+
193
+ var lat = data.latitude;
194
+
195
+ var lng = data.longitude;
196
+
197
+
198
+
199
+ var latLngInput = new google.maps.LatLng(lat,lng);
200
+
201
+
202
+
203
+ var geocoder = new google.maps.Geocoder();
204
+
205
+
206
+
207
+ geocoder.geocode(
208
+
209
+ {latLng: latLngInput},
210
+
211
+ function(results, status){
212
+
213
+
214
+
215
+ if (status == google.maps.GeocoderStatus.OK){
216
+
217
+ address = results[0].formatted_address;
218
+
219
+ document.getElementById('result').innerHTML = address;
220
+
221
+ }else if (status == google.maps.GeocoderStatus.ZERO_RESULTS){
222
+
223
+ alert("住所が見つかりませんでした。");
224
+
225
+ }else if (status == google.maps.GeocoderStatus.ERROR){
226
+
227
+ alert("サーバ接続に失敗しました。");
228
+
229
+ }else if (status == google.maps.GeocoderStatus.INVALID_REQUEST){
230
+
231
+ alert("リクエストが無効でした。");
232
+
233
+ }else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
234
+
235
+ alert("リクエストの制限回数を超えました。");
236
+
237
+ }else if (status == google.maps.GeocoderStatus.REQUEST_DENIED){
238
+
239
+ alert("サービスが使えない状態でした。");
240
+
241
+ }else if (status == google.maps.GeocoderStatus.UNKNOWN_ERROR){
242
+
243
+ alert("原因不明のエラーが発生しました。");
244
+
245
+ }
246
+
247
+ }
248
+
249
+ );
250
+
251
+ },
252
+
253
+ function( error ){
254
+
255
+ //エラーの処理
256
+
257
+ }
258
+
259
+ );
260
+
261
+ document.getElementById('result2').innerHTML = address;
262
+
263
+ }
264
+
265
+ </script>
266
+
267
+ ```