Programing

주소에서 위도 및 경도로의 Javascript 지오 코딩이 작동하지 않음

lottogame 2020. 12. 29. 06:42
반응형

주소에서 위도 및 경도로의 Javascript 지오 코딩이 작동하지 않음


다음 지오 코딩 기능을 사용하여 텍스트 주소를 위도 및 경도 숫자로 변환하고 있지만 제대로 작동하지 않습니다. 경고는 "정의되지 않음"으로 기록됩니다.

아무도 무엇이 잘못되었는지 말할 수 있습니까?

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.latitude;
    var longitude = results[0].geometry.location.longitude;
    alert(latitude);
    } 
}); 
</script>

대신 이것을 사용해보십시오.

var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();

Google의 API를 탐색하는 것은 약간 어렵지만 여기 에 관련 문서가 있습니다.

내가 찾는 데 어려움이 있었던 한 가지는 다른 방향으로가는 방법이었습니다. 좌표에서 주소로. 다음은 내가 사용하는 neded 코드입니다. 나는 또한 jquery를 사용하지 마십시오.

$.each(results[0].address_components, function(){
    $("#CreateDialog").find('input[name="'+ this.types+'"]').attr('value', this.long_name);
});

내가하는 일은 반환 된 모든 항목을 반복하고 address_components해당 유형이 양식에있는 입력 요소 이름과 일치하는지 테스트하는 것입니다. 그리고 만약 그렇다면 요소의 값을 값으로 설정합니다 address_components.
형식이 지정된 전체 주소에만 끼어있는 경우 Google의 예를 따를 수 있습니다.


위도와 경도에 잘못 액세스하고 있습니다.

시험

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    alert(latitude);
  } 
}); 
</script>

API에 대한 스크립트 태그가 최근 변경되었습니다. 이와 같은 것을 사용하여 Geocoding API를 쿼리하고 JSON 개체를 다시 가져옵니다.

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/geocode/json?address=THE_ADDRESS_YOU_WANT_TO_GEOCODE&key=YOUR_API_KEY"></script>

주소는 다음과 같을 수 있습니다.

1600 + Amphitheatre + Parkway, + Mountain + View, + CA (URI 인코딩 됨, Google에서 확인해야합니다. 매우 유용함)

또는 단순히

1600 Amphitheatre Parkway, 마운틴 뷰, CA

이 주소 https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY를 내 API Key 와 함께 브라우저에 입력하면 CA Moutain view시의 위도 및 경도 가 포함 된 JSON 객체가 반환됩니다 .

{"results" : [
  {
     "address_components" : [
        {
           "long_name" : "1600",
           "short_name" : "1600",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Amphitheatre Parkway",
           "short_name" : "Amphitheatre Pkwy",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Mountain View",
           "short_name" : "Mountain View",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Santa Clara County",
           "short_name" : "Santa Clara County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "California",
           "short_name" : "CA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "94043",
           "short_name" : "94043",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
     "geometry" : {
        "location" : {
           "lat" : 37.4222556,
           "lng" : -122.0838589
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 37.4236045802915,
              "lng" : -122.0825099197085
           },
           "southwest" : {
              "lat" : 37.4209066197085,
              "lng" : -122.0852078802915
           }
        }
     },
     "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
     "types" : [ "street_address" ]
  }],"status" : "OK"}

AngularJS 와 같은 웹 프레임 워크 를 사용하면 이러한 쿼리를 쉽게 수행 할 수 있습니다.

ReferenceURL : https://stackoverflow.com/questions/5984179/javascript-geocoding-from-address-to-latitude-and-longitude-numbers-not-working

반응형