2017-05-03 4 views
-1

ジオコードが新しく、ジオコードが固定されています。現在位置を取得しようとしています。ジオコードを使用してplaceidを取得しようとしています。ジオコーダーがZERO_RESULTSによって失敗しました。下のコードは正常に動作しています。ZERO_RESULTSのためジオコーダーが失敗しました

var currentLatitude= 23.2147721; 
var currentLongitude =72.6446714; 
    function initialize() { 
     var geocoder = new google.maps.Geocoder; 

     var latlng = { 
      lat : currentLatitude, 
      lng : currentLongitude 

     }; 
     console.log("Geocoder" + latlng); 
     geocoder.geocode({ 
      'location' : latlng 
     }, function(results, status) { 
      if (status === google.maps.GeocoderStatus.OK) { 
       if (results[1]) { 
        console.log(results[1].place_id); 
       } else { 
        window.alert('No results found'); 
       } 
      } else { 
       window.alert('Geocoder failed due to: ' + status); 
       q 
      } 
     }); 
    }; 

以下のコードはエラーを表しています。

var currentLatitude,currentLongitude; 
if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position){ 
     currentLatitude = parseFloat(position.coords.latitude); 
     currentLongitude = parseFloat(position.coords.longitude); 

      console.log("Latitude:"+currentLatitude+"Longitude:"+currentLongitude); 

     }); 
    } else { 
     console.log("Geolocation is not supported by this browser."); 
    } 

function initialize() { 
     var geocoder = new google.maps.Geocoder; 

     var latlng = { 
      lat : currentLatitude, 
      lng :currentLongitude 

     }; 
     console.log("Geocoder" + latlng); 
     geocoder.geocode({ 
      'location' : latlng 
     }, function(results, status) { 
      if (status === google.maps.GeocoderStatus.OK) { 
       if (results[1]) { 
        console.log(results[1].place_id); 
       } else { 
        window.alert('No results found'); 
       } 
      } else { 
       window.alert('Geocoder failed due to: ' + status); 

      } 
     }); 
    }; 

私が使用しているsrcが

<script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=YOURKEYHERE&callback=initialize"> 
+0

任意のヘルプは大歓迎です –

答えて

3

まず私は助けしようとするでしょうが、混乱は、あなたのコードでもあります。

navigator.geolocation.getCurrentPositionは非同期関数です。別のジオコード要求を作成する前に、この関数が終了するのを待つ必要があります。

ページのロード後にinitialize関数を呼び出すのは誰ですか?

geocoder.geocode({ 
    'location' : latlng 
} 

getCurrentPositionコールバック内にある必要があります:私は、一部のことをかなり確信しています。

またreverseGeocodingのAPIで見てみましょう:

https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding

+0

はあなたにその作業をお願いします。 –

関連する問題