7

Używam google.maps.places.AutocompleteService, aby otrzymywać sugestie dotyczące wyszukiwania miejsc, ale nie mogę geokodować niektórych prognoz.Google maps API V3 - Nie można geokodować podpowiedzi autocompleteService

Przykładem tego: Kiedy szukać „burz River Mouth”, jednego z przewidywaniami wrócę to „Storms River Mouth Rest Camp, RPA”, ale adres ten nie może być geokodowane dostać długość/długość geograficzna, np .: http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth%20Rest%20Camp,%20South%20Africa&sensor=true

Czy istnieje sposób na uzyskanie wartości szerokości/długości geograficznej dla podpowiedzi autouzupełniania?

Alternatywnie, nie rozumiem, dlaczego autouzupełnianie google zwraca prognozy, których nie mogę geokodować.

Oto prosty przykład z logiki i kodu pracuję z:

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 
    }); 
} 

[edytuj] - Zobacz przykład pracuje tu: http://demos.badsyntax.co/places-search-bootstrap/example.html

Odpowiedz

9

Zastosowanie getPlacePredictions() zamiast getQueryPredictions(). Spowoduje to zwrócenie reference miejsca, którego można użyć do pobrania szczegółów za pomocą placesService.getDetails(). Szczegóły będą zawierać geometrię miejsca.

Uwaga: obiekt services jest obiektem google.maps.places.PlacesService.

+0

service.getPlacePredictions() nie zwraca niczego i akceptuje funkcję wywołania zwrotnego. W ramach tej funkcji oddzwaniania mogę uzyskać dostęp do argumentów "predictions" i "' status' ". – badsyntax

+0

Czy potrzebujesz pomocy, czy chcesz być mądralą. Spójrz na referencyjnego członka każdej prognozy, lub opcjonalnie do dokumentów, wtedy zobaczysz, gdzie znaleźć odniesienie. –

+0

Nie próbuję w ogóle być inteligentnym aleckiem. Twoje instrukcje nie mają sensu. getPlacePredictions nic nie zwraca. Nie jestem pewien, jak inaczej to ująć. Być może czegoś brakuje? Zobacz tutaj: https://google-developers.appspot.com/maps/documentation/javascript/examples/places-queryprediction – badsyntax

-1

Może to może mógłby pomóc.

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

Nie chcę używać widgetu "google.maps.places.Autocomplete', chcę użyć" google.maps.places.Usługa AutocompleteService' z moim własnym autouzupełnianiem. – badsyntax

+0

Mógłbyś przepisać css z .pac-container i .pac-item. –

+0

Nie szuka w interfejsie autouzupełniania. Są inne pytania/odpowiedzi, które to obejmują. – Raha

2

Spróbuj tego:

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); 
     } 
    }); 
} 

Następnie rozmowa i zwrotna:

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

Przepraszam za mój angielski. Mam pytanie do ciebie: Czy możesz pokazać mi metodę showInAutoComplete() ??? Pokazuję prognozy na liście href, ale nie wiem, jak zapisać "klikniętą" wartość adresu.

+0

Wielkie dzięki za wskazanie mi funkcji API geokodowania, mój problem był trochę inny, ale pomógł mi go rozwiązać! – Romko

2

Oto kod mam napisane, że jest inspirowana od @ 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()); 
    }); 

Emisji widzę to nowy obiekt PlacesService na każdym naciśnięciu przycisku, które mogą być przesadą - ja nie wiem obejść chociaż.

Umieszczanie go tutaj w przypadku, gdy ktoś go szuka.

+0

dlaczego użyłeś document.get (q) w pętli for? – vipulsodha

0

Jeśli trzeba zwrócić wszystkie wyniki naraz w odpowiedniej kolejności, użyj tego:

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

Przewidywania zwracane przez AutocompleteService ma PlaceId nieruchomości. Możesz przekazać PlaceId zamiast adresu do geokodera zgodnie z dokumentacją https://developers.google.com/maps/documentation/javascript/geocoding.

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); 
      } 
     }); 
    } 
}); 
Powiązane problemy