2011-04-11 8 views
7

다음 예인 http://jsbin.com/inepo3/7/edit으로 Google Maps API의 lng 및 lat 좌표를 가져 오려고합니다. '성공'팝업이 표시되기를 기대하지만 '오류'팝업이 계속 표시됩니다. google maps-request는 정확한 json 피드백을 제공합니다 (방화 광으로 확인).ajax() 요청으로 Google Maps API의 주소 좌표 가져 오기

<script type="text/javascript"> 
    $().ready(function() { 

     $.fn.getCoordinates=function(address){ 

      $.ajax(
      { 
       type : "GET", 
       url: "http://maps.google.com/maps/api/geocode/json", 
       dataType: "jsonp", 
       data: { 
        address: address, 
        sensor: "true" 
       }, 
       success: function(data) { 
        set = data; 
        alert(set); 
       }, 
       error : function() { 
        alert("Error."); 
       } 
      }); 
     }; 

     $().getCoordinates("Amsterdam, Netherlands"); 
    }); 
</script> 

누구든지이 문제를 해결하는 방법을 알고 있습니까?

<script type="text/javascript"> 
    $().ready(function() { 
     var user1Location = "Amsterdam, Netherlands"; 
     var geocoder = new google.maps.Geocoder(); 
     //convert location into longitude and latitude 
     geocoder.geocode({ 
      address: user1Location 
     }, function(locResult) { 
      console.log(locResult); 
      var lat1 = locResult[0].geometry.location.lat(); 
      var lng1 = locResult[0].geometry.location.lng(); 
      $("#testDiv").html("latitude:" + lat1 + "<p>longitude:" + lng1 + "</p>"); 
     }); 
    }); 
</script> 
+0

구글 맵 API의 버전을 : 우리는 기본적으로 트윗의 주소 (. IE 런던, 마드리드 또는 등 조지아)를 얻을 구글지도의 지오 코더 서비스를 사용하는 LatLng에 실제 주소를 변환하기 위해 트위터를 사용합니까? – kjy112

+0

나는 shure 아니야, 아마 v2 (아마도 사용 된지도 URL을 버전으로 변환 할 수 있겠는가?) –

+0

나는 V3가 지금 다시 그것을보고 있다는 것을 확신한다. – kjy112

답변

9

구글지도 API V3는 외부 라이브러리 어렵게 만드는 :

감사합니다, 귀도 Lemmens

편집

나는 jQuery를 결합 Google지도 자바 스크립트 API를 사용하여 bether 솔루션을 발견 JSONP로 작업 할 수 있습니다. 다음은 블로그 게시물입니다.

JSONP and Google Maps API Geocoder Plus A Fix w/ jQuery


지오 코딩을 얻기의 또 다른 방법은 Google Map V3 API Geocoder Service을 사용하는 것입니다. 다음은 Google지도 V3 지오 코더 서비스를 사용하기 위해 JSONP를 대체 할 때 비슷한 문제가있는 사용자를 도왔다는 예입니다. 이것 좀 봐 JSFiddle Demo:

이것은 기본적으로 핵심입니다.

$.getJSON(
    url1, function(results) { // get the tweets 
     var res1 = results.results[0].text; 
     var user1name = results.results[0].from_user; 
     var user1Location = results.results[0].location; 

     // get the first tweet in the response and place it inside the div 
     $("#last-tweet1").html(res1 + "<p>from: " + user1name + " (" + user1Location + ")</p><p>"); 
     //convert location into longitude and latitude 
     geocoder.geocode({ 
      address: user1Location 
     }, function(locResult) { 
      console.log(locResult); 
      var lat1 = locResult[0].geometry.location.lat(); 
      var lng1 = locResult[0].geometry.location.lng(); 
      $("#testDiv").html("latitude:" + lat1 + "<p>longitude:" + lng1 + "</p>"); 
     }); 
    }); 
+1

차갑고, 나는 당신의 예를 풀어 내 원래 게시물에 추가했습니다. 이것은 완벽하게 작동합니다! –

+0

매우 유용합니다. 감사합니다! – montrealist

+0

crossdomain 오류가 발생하는 이유는 무엇입니까? –

관련 문제