2016-12-15 14 views
-1

私はGoogleのジオコーダJSのAPIから緯度と経度を取得するために、有効なAPIキーで、次のコードを使用しています:GoogleのジオロケーションJS APIを与えるナンセンス緯度/経度

<script async defer type="text/javascript" 
    src="http://maps.google.com/maps/api/js?key=[key]"> 
</script> 
<script> 
    var geocoder = new google.maps.Geocoder(); 
    var address = "1600 Amphitheatre Parkway, Mountain View, CA"; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      console.log (results[0]); 
      // results[0].geometry.location.lat 
      // results[0].geometry.location.lng 
     } 
     else console.log(status, results); 
    }); 
</script> 

クエリをGoogleにサーバーは正常に動作し、結果を戻します。問題は、私が入力したアドレスに関係なく、location.lat_.E/this.lat()に戻り、location.lng_.E/this.lng()に戻ってくることです。ビューポートの座標はきれいですが、実際の緯度と経度の結果はナイスセンスです。コードを関数に入れてコールバックとして渡すと、同じことが起こります。

誰もこれまでにこれまでに遭遇したことはありますか?私は行方不明のものがありますか?私が検索したときにこの問題についてはどこにも何も見つかりませんでした。これはAPIを初めて使用したときです。

答えて

5

results[0].geometry.locationは、google.maps.LatLngである。それは.lat/.lng性質を持っていない、彼らは機能している、あなたはそれらを呼び出す必要があります:

var geocoder = new google.maps.Geocoder(); 
    var address = "1600 Amphitheatre Parkway, Mountain View, CA"; 
    geocoder.geocode({ 
    'address': address 
    }, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     console.log(results[0]); 
     var lat = results[0].geometry.location.lat(); 
     var lng = results[0].geometry.location.lng(); 
     map.setCenter(results[0].geometry.location); 
    } else console.log(status, results); 
    }); 

proof of concept fiddle

コードスニペット:

var geocoder; 
 
var map; 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var geocoder = new google.maps.Geocoder(); 
 
    var address = "1600 Amphitheatre Parkway, Mountain View, CA"; 
 
    geocoder.geocode({ 
 
    'address': address 
 
    }, function(results, status) { 
 
    if (status == google.maps.GeocoderStatus.OK) { 
 
     console.log(results[0]); 
 
     var lat = results[0].geometry.location.lat(); 
 
     var lng = results[0].geometry.location.lng(); 
 
     var iw = new google.maps.InfoWindow(); 
 
     iw.setContent("lat:" + lat + "<br>lng:" + lng); 
 
     iw.setPosition(results[0].geometry.location); 
 
     iw.open(map); 
 
     map.setCenter(results[0].geometry.location); 
 
    } else console.log(status, results); 
 
    }); 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>

+0

私は私はそれらを警告してみたと誓っていた可能性があります広告はそれが本当ではないと不平を言ったが、私はもう一度それを試して、それは働いた。奇妙な。ありがとう。 – DiMono

関連する問題