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