2012-02-27 6 views
3

내가 주소를 검색하려면이 코드를 사용하는 웹 응용 프로그램을 짓고 있어요로부터 위도와 경도를 추출하는 방법 :이Google지도 자동 완성

http://code.google.com/intl/en/apis/maps/documentation/javascript/examples/places-autocomplete.html

유형, 뉴욕, 뉴욕을.

사용자가 자동 ​​완성 옵션을 사용하면지도가 DOM에로드 될 때 사용자는 데이터베이스에 위치 주소를 저장할 수 있으며 "예 : New York, NY"와 같이 사용할 수 있습니다. 그러나 응용 프로그램을 위해 위도와 경도도 저장해야합니다.

그러나 나는 Google API에서 그들을 잡아낼 수있는 아이디어가 없습니다.

테스트 응용 프로그램으로, 나는 여전히 Google 코드를 사용하고 있습니다. 사용자가 주소를 선택하면 숨겨진 필드를 만들어 위도와 경도를 지정해야합니다.

내 문제의 구현은 환영합니다! 사전에

감사

P.S : I에 유래에 새로 온 사람으로 내가 나 자신을 대답 could't. 그래서 내가 유래에 의해 제안, 게시물을 편집, 여기에 다니엘의 대답에 본사를 둔 솔루션 및 Google API의 일부 연구가있어 :

function getLatLng(address) 
{ 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address' : address }, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var mapLatLng = document.getElementById('mapLatLng'); // mapLatLng is my hidden field, use your own 
      mapLatLng.value = results[0].geometry.location.lat() + ', ' + results[0].geometry.location.lng(); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 

    });    
} 

답변

6

당신은 경도를 얻기 위해 구글 API를 사용할 수 있으며 주소에서 가져온 위치 찾기. 이미 말했듯이 결과를 삽입해야하는 숨겨진 필드를 구현해야합니다. 그런 다음 좌표와 함께 위치를 저장할 수 있습니다.

나는 최근에 내 프로젝트 중 하나에이 기능을 구현

:

$('#latitude').val(results[0].geometry.location.Pa) 
$('#longitude').val(results[0].geometry.location.Qa) 

당신에게 :

function getLatLngFromAddress(city, country){ 

    var address = city +", "+ country; 
    var geocoder = new google.maps.Geocoder(); 

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

    if (status == google.maps.GeocoderStatus.OK) { 
     $('#latitude').val(results[0].geometry.location.lat()); 
     $('#longitude').val(results[0].geometry.location.lng()); 

    } else { 
     console.log("Geocode was not successful for the following reason: " + status); 
    } 
    }); 
} 
+0

코드를 구현했지만 succ가 없습니다. ess !!! –

10

다니엘은 자신이 잘못 대부분의 경우, 적합한 여기에 재산의 그의 접근입니다 대신 제공된 lat() 및 lng() 함수를 사용해야합니다.

$('#latitude').val(results[0].geometry.location.lat()) 
$('#longitude').val(results[0].geometry.location.lng()) 
관련 문제