将 Google 个地点限制在 JAVA 个国家/地区

Restrict Google Places to country in JAVA

我正在使用 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 地点自动完成只会 return 最多五个结果:也许您正在寻找可以在US 和 API 是 return 将它们排在首位,这可能与问题有关。

此外,请尝试向 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")
);