问题描述
我正在使用 GooglePlaces 获取地点
myController.java
@GetMapping("/connexionFR")
public String displayLoginPageFR(Model model) {
...
client = new GooglePlaces("MY_KEY_GOOGLE");
...
return "/FR/MDB_VTC.html";
}
@GetMapping("/search")
public ResponseEntity<String> doAutoComplete(@RequestParam("q") final String input) {
List<String> proposition = new ArrayList<>();
List<String> strings = new ArrayList<>();
List<Prediction> predictions = client.getPlacePredictions(input);
for(Prediction p : predictions) {
proposition.add(p.getDescription());
}
strings = getStrings(input,proposition) ;
ObjectMapper mapper = new ObjectMapper();
String resp = "";
try {
resp = mapper.writeValueAsstring(strings);
} catch (JsonProcessingException e) {
}
return new ResponseEntity<String>(resp,HttpStatus.OK);
}
myHTML.html
...
<input type="text" name="search" id="searchBox" style="width: 560px; margin: auto;" />
...
<script>
$(function() {
$("#searchBox").autocomplete({
source : function(request,response) {
$.ajax({
url : "http://localhost:8092/search",dataType : "json",data : {
q : request.term
},success : function(data) {
//alert(data);
console.log(data);
response(data);
}
});
},minLength : 1
});
});
</script>
...
我只得到美国的结果,我想得到其他国家的结果。
解决方法
如问题中提供的评论所示,可能正在使用 google-places-api-java 库。
如 API documentation 中所述,请注意,Google 地方信息自动完成最多只会返回五个结果:也许您正在寻找可以在美国找到的地点,而 API 会首先返回这些结果,可能与问题有关。
此外,请尝试向 API 提供您想要获取结果的国家/地区。您可以使用 components
参数执行此操作,例如:
List<Prediction> predictions = client.getPlacePredictions(
input,Param.name("components").value("country:fr")
);
您也可以提供国家/地区列表:
List<Prediction> predictions = client.getPlacePredictions(
input,Param.name("components").value("country:fr|country:dz")
);