2016-09-23 7 views
0

GoogleプレイスAPIサービス(https://developers.google.com/places/web-service/)を使用して、場所名に基づいてプレイスIDを取得しています。ユーザーがいくつかの場所名を入力すると、ユーザーが選択して場所IDを取得できる関連する候補が表示されます。私は3つのテキストボックスとボタンが必要な要件があります。ユーザーはPlace Name、City Name、およびAddressを入力し、ボタンをクリックしてPlaceIdを取得します。今は、複数のパラメータに基づいてプレースIDを取得するオプションはありません。どうすればそれを達成できますか?GoogleプレイスAPIサービスのプレイス名、都市名、住所に基づいてプレイスIDを取得する

答えて

3

Text Search Requestsクエリ文字列を受け入れます。 1つの潜在的な解決策は、ユーザーが異なるテキストフィールドに場所名、都市名、住所を入力できるようにすることですが、Ajaxリクエストを送信する前にすべてを1つのクエリ文字列に連結します。

<form id="form"> 
    <input type="text" name="place" value=""> 
    <input type="text" name="city" value=""> 
    <input type="text" name="address" value=""> 
    <input type="submit" value="Locate"> 
</form> 

使いのブラウザではJavaScriptが次のようになります:

$("#form").submit(function(event) { 

    // concatenate places into a single query string 
    var query = $('input[name="place"]').val() + $('input[name="city"]').val() + $('input[name="address"]').val(); 

    // convert spaces to '+' symbol for query 
    query = encodeURIComponent(query); 

    // send ajax request 
    $.ajax({url: "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=" + query + "&key=YOUR_API_KEY", success: function(result){ 
     alert('success, now retrieve your id from result variable'); 
    }}); 

    //prevent the submit button from reloading the page 
    event.preventDefault(); 
}); 

フォームは次のようになります

関連する問題