2011-04-11 26 views
7

द्वारा Google मानचित्र API के पता निर्देशांक प्राप्त करें, मैं अगले उदाहरण http://jsbin.com/inepo3/7/edit द्वारा Google मानचित्र API के एलएनजी और लैट निर्देशांक प्राप्त करने का प्रयास कर रहा हूं। मैं एक 'सफलता' पॉपअप की उम्मीद करता हूं, लेकिन यह 'त्रुटि' पॉपअप दिखाता रहता है। Google मानचित्र-अनुरोध सही जेसन प्रतिक्रिया देता है (फ़ायरबग द्वारा चेक किया गया)।AJAX() अनुरोध

<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

जो गूगल मानचित्र एपीआई के संस्करण? – kjy112

+0

मैं शर्मिंदा नहीं हूं, मुझे लगता है कि v2 (क्या आप प्रयुक्त मानचित्र यूआरएल को किसी संस्करण में परिवर्तित कर सकते हैं?) –

+0

मुझे पूरा यकीन है कि यह V3 अब इसे फिर से देख रहा है। – kjy112

उत्तर

9

गूगल मानचित्र एपीआई V3 बाहरी पुस्तकालयों के लिए यह कठिन बना देता है:

सादर, गुइडो Lemmens

संपादित

मैं एक bether समाधान Google मानचित्र Javascript API jQuery में संयुक्त का उपयोग कर पाया जेएसओएनपी के साथ काम करने के लिए। इसके बारे में यहां एक ब्लॉग पोस्ट है।

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


जियोकोडिंग होने का एक वैकल्पिक तरीका Google Map V3 API Geocoder Service उपयोग करने के लिए है। यहां एक उदाहरण दिया गया है कि मैंने एक ऐसे व्यक्ति की मदद की जिसमें आपके JSONP को Google Map V3 Geocoder सेवा का उपयोग करने के लिए एक समान समस्या हो रही थी। इस JSFiddle Demo:

पर मूल रूप से मूल देखें। हम मूल रूप से चहचहाना का उपयोग ट्वीट के पते (आईई लंदन, मैड्रिड या जॉर्जिया आदि।) मिलता है और गूगल मानचित्र की जियोकोडर सेवा का उपयोग कर LatLng में वास्तविक पते कन्वर्ट करने के लिए:

$.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

क्रॉसडोमेन त्रुटि प्राप्त करें, क्यों? –