2016-09-23 70 views
0

我正在使用Google Places API服务(https://developers.google.com/places/web-service/)根据地点名称获取地点ID。用户输入一些位置名称并出现相关的建议,用户可以从中选择并获取地点Id。我有一个要求,我需要有三个文本框和一个按钮。用户将输入地名,城市名称和地址,然后点击按钮以获取placeId。目前,我没有看到根据多个参数获取地点ID的选项。我怎么能做到这一点?根据Google Places API服务中的地点名称,城市名称和地址获取地点ID

回答

3

Text Search Requests接受查询字符串。一个可能的解决方案是让用户输入地名,城市和地址到不同的文本字段中,然后在发送您的ajax请求之前将所有这些字符串连接成一个查询字符串。

你的形式看起来是这样的:

<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(); 
}); 
相关问题