如何获得自动完成地图的结果只有一个国家? [英] How to get results of autocomplete maps only one country?

查看:100
本文介绍了如何获得自动完成地图的结果只有一个国家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取自动填充地图的结果只有一个国家/地区

你没有指定你正在使用的API。根据 Google Places API > docs 有一个可选的组件参数。来自文档:


组件 - 您想要限制
结果的一组地方。目前,您可以使用组件按国家进行过滤。
该国家必须以ISO 3166-1 Alpha-2
兼容国家代码的双字符形式通过。例如:components = country:fr would
将结果限制在法国境内。


这意味着您只需添加& components = country:fr 到您的网址并自动完成限于法国。


$ b 如果您使用 Google maps Js API 的 PlacesService 您可以使用 componentsRestriction 参数。有一个国家属性,根据文件:


限制预测指定的国家(ISO 3166-1 Alpha-2
国家代码,不区分大小写)。例如,us,br,au。

编辑

如果你还没有选择API,那么这里有一个简单的例子(注意这行: country:'us',你将不得不输入你想要的国家代码列表此处。需要小写!):



在此处输入位置:< input id =searchTextFieldtype = textsize =50>< script src ='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'>< / script>< script src =https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places>< / script>


How to get results of autocomplete maps only one country?

解决方案

You didn't specify which API you are using. Using Google Places API, according to the docs there is an optional components parameter. From docs:

components — A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by country. The country must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France.

That means you will just add &components=country:fr to your url and autocomplete is restricted to france.

If you use Google maps Js API's PlacesService you can restrict the results of google maps places autocomplete using componentsRestriction parameter. There is a country attribute, which according to the docu:

Restricts predictions to the specified country (ISO 3166-1 Alpha-2 country code, case insensitive). E.g., us, br, au.

EDIT

If you didn't choose API yet, here is a simple example (Notice this line: country: 'us' , you will have to input your desired country code there. List of codes here. Needs to be lower case!):

var pac_input = document.getElementById('searchTextField');


$(function() {
  // Initialize Autocomplete
  var options = {
    componentRestrictions: {
      country: 'us' //PUT YOUR COUNTRY CODE HERE!!
    }
  };
  var autocomplete = new google.maps.places.Autocomplete(pac_input, options);
});

Input location here:<input id="searchTextField" type="text" size="50">
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

这篇关于如何获得自动完成地图的结果只有一个国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆