【问题标题】:Getting Place ID based on Place Name, City Name and Address from Google Places API service从 Google Places API 服务获取基于地点名称、城市名称和地址的地点 ID
【发布时间】:2016-09-23 01:32:33
【问题描述】:
【问题讨论】:
标签:
api
google-places-api
【解决方案1】:
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();
});