7

मैं स्थान खोज के लिए सुझाव प्राप्त करने के लिए google.maps.places.AutocompleteService का उपयोग कर रहा हूं, लेकिन मैं कुछ भविष्यवाणियों को geocode नहीं कर सकता।Google मानचित्र API V3 - AutocompleteService भविष्यवाणियों को geocode नहीं कर सकता

इस का एक उदाहरण: जब मैं 'तूफान नदी मुंह', भविष्यवाणियों मैं वापस पाने के लिए खोज करते 'तूफान नदी मुँह रेस्ट कैंप, दक्षिण अफ्रीका' है, लेकिन इस पते पाने के लिए जियोकोड नहीं किया जा सकता है लैटूड/रेखांश, उदाहरण: http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth%20Rest%20Camp,%20South%20Africa&sensor=true

क्या स्वत: पूर्ण भविष्यवाणियों के लिए अक्षांश/देशांतर मूल्य प्राप्त करने का कोई तरीका है?

वैकल्पिक रूप से, मुझे समझ में नहीं आता कि क्यों Google स्वत: पूर्ण भविष्यवाणियों को वापस कर रहा है कि मैं geocode नहीं कर सकता।

यहाँ तर्क और कोड की एक बुनियादी उदाहरण मैं के साथ काम कर रहा हूँ है:

var geocoder = new google.maps.Geocoder(); 
var service = new google.maps.places.AutocompleteService(null, { 
    types: ['geocode'] 
}); 

service.getQueryPredictions({ input: query }, function(predictions, status) { 
    // Show the predictions in the UI 
    showInAutoComplete(predictions); 
}; 

// When the user selects an address from the autcomplete list 
function onSelectAddress(address) { 
    geocoder.geocode({ address: address }, function(results, status) { 
    if (status !== google.maps.GeocoderStatus.OK) { 
     // This shouldn't never happen, but it does 
     window.alert('Location was not found.'); 
    } 
    // Now I can get the location of the address from the results 
    // eg: results[0].geometry.location 
    }); 
} 

[संपादित करें] - एक काम उदाहरण यहाँ देखें: http://demos.badsyntax.co/places-search-bootstrap/example.html

उत्तर

9

उपयोग getPlacePredictions() बजाय getQueryPredictions()। यह उस स्थान के लिए reference वापस करेगा, जिसका उपयोग आप placesService.getDetails() का उपयोग करके विवरण पुनर्प्राप्त करने के लिए कर सकते हैं। विवरण में जगह के लिए ज्यामिति होगी।

नोट: placesService एक google.maps.places.PlacesService-object है।

+0

service.getPlace भविष्यवाणियां() कुछ भी वापस नहीं करती है, और यह कॉलबैक फ़ंक्शन स्वीकार करती है। उस कॉलबैक फ़ंक्शन के भीतर मैं 'भविष्यवाणियों' और 'स्थिति' 'तर्कों तक पहुंच सकता हूं। – badsyntax

+0

क्या आप मदद चाहते हैं या आप एक स्मार्ट एलेक बनना चाहते हैं। प्रत्येक भविष्यवाणी के संदर्भ-सदस्य, या वैकल्पिक रूप से दस्तावेज़ों में एक नज़र डालें, फिर आप देखेंगे कि संदर्भ कहां प्राप्त करें। –

+0

मैं बिल्कुल एक स्मार्ट एलेक बनने की कोशिश नहीं कर रहा हूं। आपके निर्देश समझ में नहीं आता है। getPlace भविष्यवाणियां कुछ भी वापस नहीं करता है। मुझे यकीन नहीं है कि इसे और कैसे रखा जाए। शायद मुझे कुछ याद आ रहा है? यहां देखें: https://google-developers.appspot.com/maps/documentation/javascript/examples/places-queryprediction – badsyntax

-1

हो सकता है कि यह तुम्हारी मदद कर सकता सकता है।

var autocomplete = new google.maps.places.Autocomplete(this.input); 
//this.input is the node the AutocompleteService bindTo 
autocomplete.bindTo('bounds', this.map); 
//this.map is the map which has been instantiated 

google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    var place = autocomplete.getPlace(); 
    //then get lattude and longitude 
    console.log(place.geometry.location.lat()); 
    console.log(place.geometry.location.lng()); 
} 
+1

मैं 'google.maps.places.Autocomplete' UI विजेट का उपयोग नहीं करना चाहता, मैं' google.maps.places का उपयोग करना चाहता हूं।AutocompleteService' सेवा अपने स्वयं के कस्टम स्वत: पूर्ण के साथ। – badsyntax

+0

आप .pac-container और .pac-item के css को फिर से लिख सकते हैं। –

+0

वह स्वत: पूर्ण यूआई का उपयोग नहीं कर रहा है। ऐसे अन्य प्रश्न/उत्तर हैं जो इसे कवर करते हैं। – Raha

2

इस प्रयास करें:

function onSelectAddress(address, callback) { 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      callback(results[0].geometry.location); 
     } else { 
      alert("Can't find address: " + status); 
      callback(null); 
     } 
    }); 
} 

फिर, कॉल और कॉलबैक: मेरी अंग्रेजी के लिए

onSelectAddress('your address here', function(location){ 
//Do something with location 
if (location) 
    alert(location); 
}); 

क्षमा करें। मुझे आपके लिए एक प्रश्न मिला: क्या आप मुझे विधि showInAutoComplete() ??? मैं href की सूची पर भविष्यवाणियां दिखा रहा हूं, लेकिन मुझे नहीं पता कि 'क्लिक किए गए' पता मूल्य को कैसे बचाया जाए। मैं नहीं जानता कि -

+0

मुझे जियोकोडिंग एपीआई को इंगित करने के लिए बहुत कुछ धन्यवाद, मेरी समस्या थोड़ा अलग थी, लेकिन इससे मुझे इसे हल करने में मदद मिली! – Romko

2

यहाँ कोड मैं लिखा है कि Dr.Molle

function initializePlaces(q) { 
     googleAutocompleteService = new google.maps.places.AutocompleteService();    
     if(q){ 
      googleAutocompleteService.getPlacePredictions({ 
       input: q 
      }, callbackPlaces); 
     }else { //no value entered loop } 
    } 

    function callbackPlaces(predictions, status) { 
     if (status != google.maps.places.PlacesServiceStatus.OK) { 
      alert(status); 
      return; 
     } 

     for (var i = 0; i < predictions.length; i++) { 
      googlePlacesService = new google.maps.places.PlacesService(document.getElementById("q")); 
      googlePlacesService.getDetails({ 
       reference: predictions[i].reference 
      }, function(details, status){ 
       if(details){ 
        console.log(details.geometry.location.toString()); 
       } 
      }); 
      console.log(predictions[i].description); 
     } 
    }; 

    google.maps.event.addDomListener(window, 'load', initializePlaces); 

    $(document).on('keyup', 'input#q', function(e){ 
     initializePlaces($(this).val()); 
    }); 

समस्या मैं देख रहा हूँ कि हर कुंजी दबाने पर एक नया PlacesService उद्देश्य यह है कि एक overkill हो सकता है @ से प्रेरित है यद्यपि काम करते हैं।

यहां पोस्ट करने के मामले में कोई इसे ढूंढ रहा है।

+0

आपने लूप के लिए document.get (q) का उपयोग क्यों किया? – vipulsodha

0

आप उचित क्रम में एक बार में सभी परिणाम वापस की जरूरत है, इस का उपयोग करें:

var service = new google.maps.places.AutocompleteService(); 
service.getPlacePredictions({ 
    input: '*** YOUR QUERY ***' 
}, function(predictions, status) { 
    var data = []; 

    if (status != google.maps.places.PlacesServiceStatus.OK) { 
     console.error(status); 
     processResults(data); 
     return; 
    } 

    var s = new google.maps.places.PlacesService(document.createElement('span')); 
    var l = predictions.length; 
    for (var i = 0, prediction; prediction = predictions[i]; i++) { 
     (function(i) { 
      s.getDetails(
       { 
        reference: prediction.reference 
       }, 
       function (details, status) { 
        if (status == google.maps.places.PlacesServiceStatus.OK) { 
         data[i] = details; 
        } else { 
         data[i] = null; 
        } 
        if (data.length == l) { 
         processResults(data); 
        } 
       } 
      ); 
     })(i); 
    } }); 

function processResults(data) { 
    console.log(data); 
} 
1

भविष्यवाणियों AutocompleteService द्वारा लौटाए गए एक PlaceId संपत्ति है। प्रलेखन https://developers.google.com/maps/documentation/javascript/geocoding के अनुसार आप geocoder के पते के बजाय PlaceId पास कर सकते हैं।

var service = new google.maps.places.AutocompleteService(); 
var request = { input: 'storms river mouth' }; 
service.getPlacePredictions(request, function (predictions, status) { 
    if(status=='OK'){ 
     geocoder.geocode({ 
      'placeId': predictions[0].place_id 
     }, 
     function(responses, status) { 
      if (status == 'OK') { 
       var lat = responses[0].geometry.location.lat(); 
       var lng = responses[0].geometry.location.lng(); 
       console.log(lat, lng); 
      } 
     }); 
    } 
});