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

質問編集履歴

1

追記

2020/08/21 09:23

投稿

ringoame49
ringoame49

スコア46

title CHANGED
File without changes
body CHANGED
@@ -73,4 +73,62 @@
73
73
  あってもなくても結果は変わりません。
74
74
 
75
75
  以上、なぜグローバル変数が表示されないのかとvar address = "";の記述が必要なのかをご教授いただけますと幸いです。
76
- よろしくお願いいたします。
76
+ よろしくお願いいたします。
77
+
78
+ 追記
79
+ 現在のコード
80
+ ```
81
+ <body>
82
+ <div id="result"></div>
83
+ <div id="result2"></div>
84
+ </body>
85
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
86
+ <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXXXXX"></script>
87
+ <script type="text/javascript">
88
+
89
+ if( navigator.geolocation ){
90
+
91
+ navigator.geolocation.getCurrentPosition(
92
+
93
+ function( position ){
94
+
95
+ var data = position.coords;
96
+
97
+ var lat = data.latitude;
98
+ var lng = data.longitude;
99
+
100
+ var latLngInput = new google.maps.LatLng(lat,lng);
101
+
102
+ var geocoder = new google.maps.Geocoder();
103
+
104
+ geocoder.geocode(
105
+ {latLng: latLngInput},
106
+ function(results, status){
107
+
108
+ if (status == google.maps.GeocoderStatus.OK){
109
+ address = results[0].formatted_address;
110
+ document.getElementById('result').innerHTML = address;
111
+ }else if (status == google.maps.GeocoderStatus.ZERO_RESULTS){
112
+ alert("住所が見つかりませんでした。");
113
+ }else if (status == google.maps.GeocoderStatus.ERROR){
114
+ alert("サーバ接続に失敗しました。");
115
+ }else if (status == google.maps.GeocoderStatus.INVALID_REQUEST){
116
+ alert("リクエストが無効でした。");
117
+ }else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
118
+ alert("リクエストの制限回数を超えました。");
119
+ }else if (status == google.maps.GeocoderStatus.REQUEST_DENIED){
120
+ alert("サービスが使えない状態でした。");
121
+ }else if (status == google.maps.GeocoderStatus.UNKNOWN_ERROR){
122
+ alert("原因不明のエラーが発生しました。");
123
+ }
124
+ }
125
+ );
126
+ },
127
+ function( error ){
128
+ //エラーの処理
129
+ }
130
+ );
131
+ document.getElementById('result2').innerHTML = address;
132
+ }
133
+ </script>
134
+ ```