使用谷歌地图API中的Locationbias查找广场上的所有商店

2024-04-27 02:47:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近刚开始使用GoogleMapsAPI,遇到了一个似乎无法解决的问题

我想使用Google Maps API和find\u placelocation\u bias查找特定区域内的所有商店。如果我使用places.neared和半径,我的代码就可以工作。但是,如果我改为使用矩形指定区域,如下所示,则它不起作用:

gmaps = googlemaps.Client(key = API_Key)

results = gmaps.find_place('-34.627041, -58.479310', input_type= 'textquery', location_bias= ("rectangle" ['-34.627041, -58.479310'|'-34.625743, -58.478704']))

错误消息:

TypeError: unsupported operand type(s) for |: 'str' and 'str'

你知道怎么做吗


Tags: api区域typegoogleplacelocationfind商店
1条回答
网友
1楼 · 发布于 2024-04-27 02:47:56

我认为您只需要稍微调整一下请求的格式。您不希望使用“查找位置”搜索lat/long。您将希望遵循此参考: https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/test/test_places.py

我想我能够重新创建您尝试在此处执行的搜索:

pip install -U googlemaps
import googlemaps
gmaps = googlemaps.Client(key = YOUR_API_KEY)
results = gmaps.find_place("doctor", "textquery", fields=["name", "geometry"], location_bias="rectangle:-34.627041, -58.479310|-34.625743, -58.478704")
print(results)

我的结果是:

{'candidates': [{'geometry': {'location': {'lat': -34.625202, 'lng': -58.476802}, 'viewport': {'northeast': {'lat': -34.62389142010728, 'lng': -58.47543712010729}, 'southwest': {'lat': -34.62659107989272, 'lng': -58.47813677989273}}}, 'name': 'KANG HI YOUNG'}], 'status': 'OK'}

编辑: 这里是一个Places文本搜索。请注意,使用这种搜索时,您只能按位置/半径限制搜索结果(因此,将搜索结果限制为圆形而不是长方体)。还要注意,您无法控制接收的字段,因此每个请求都会为所有可能的字段计费。 https://developers.google.com/places/web-service/search#TextSearchRequests

在这个例子中,我正在搜索类型为“restaurant”的地方,使用查询“pizza”,并将其限制在纽约时代广场25米以内。警告,即使半径只有25米,仍然有很多地方,所以我不会在答案中包含全部结果

pip install -U googlemaps
import googlemaps
gmaps = googlemaps.Client(key = YOUR_API_KEY)
results = gmaps.places("pizza", location="40.758896, -73.985130", radius=25, type="restaurant")
print(results["results"])

我的结果是:

[{'formatted_address': '260 W 44th St, New York, NY 10036', 'geometry': {'location': {'lat': 40.7582256, 'lng': -73.9883779}, 'viewport': {'northeast': {'lat': 40.75963837989272, 'lng': -73.98702852010726}, 'southwest': {'lat': 40.75693872010727, 'lng': -73.98972817989271}}}, 'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png', 'id': 'df91ea6f45a91e87bc34513f138d7d8ce4839cca', 'name': "John's of Times Square", 'opening_hours': {'open_now': True}, 'photos': [{'height': 3456, 'html_attributions': ['<a href="https://maps.google.com/maps/contrib/117567420502995919728">John&#39;s of Times Square</a>'], 'photo_reference': 'CmRaAAAAqswJiOIto9LdQXBKavvfKSjngfP9awml-4_Bb96zQ97wnEe52d_lI5-PunOByiCZvxAD_tDiOM7RF7V1pG-bW93QrHK2Wi47H7duDoNsLzp1W7wBnTG53MGv3ljktwhPEhB3fcInrMvXqGD68iDf2gA1GhSXNKTlwdz8YI7xgd5CSdwxoVAP1A', 'width': 5184}], 'place_id': 'ChIJBSESh1RYwokRyHcVnrG7JWo', 'plus_code': {'compound_code': 'Q256+7J New York', 'global_code': '87G8Q256+7J'}, 'price_level': 2, 'rating': 4.3, 'reference': 'ChIJBSESh1RYwokRyHcVnrG7JWo', 'types': ['restaurant', 'food', 'point_of_interest', 'establishment'], 'user_ratings_total': 5097}, {'formatted_address': '142 W 49th St, New York, NY 10019', 'geometry': {'location': {'lat': 40.7599775, 'lng': -73.982976}, 'viewport': {'northeast': {'lat': 40.76139582989272, 'lng': -73.98157242010727}, 'southwest': {'lat': 40.75869617010728, 'lng': -73.9842720798927}}}, 'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png', 'id': '7b62a154edc6af4a4adc1400f94f6385e0ff155f', 'name': 'Radio City Pizza', 'opening_hours': {'open_now': True}, 'photos': [{'height': 2988, 'html_attributions': ['<a href="https://maps.google.com/maps/contrib/110218252588412989073">Shauna Hart</a>'], 'photo_reference': 'CmRaAAAAj3VvSZBhvCDz0tRmsalKj6HUMN8VrFl-80WSQmlobtoXDO-2IciUrPzkkhgGTnWG-e286sExAu6Y95Vne_w4ttkiXmwG99pSb9hwYmAWIhfd8wzv2ga1Zd8cYeivSIG1EhCfwXwwqGft_IAJovCZkJ5cGhSJbzEw-KrLJ_jvHXZfMQGqLrKXSA', 'width': 5312}], 'place_id': 'ChIJuaKH_1VYwokR3TwIUoG8mDo', 'plus_code': {'compound_code': 'Q258+XR New York', 'global_code': '87G8Q258+XR'}, 'price_level': 1, 'rating': 4.1, 'reference': 'ChIJuaKH_1VYwokR3TwIUoG8mDo', 'types': ['restaurant', 'food', 'point_of_interest', 'establishment'], 'user_ratings_total': 301}, ... plus way more

相关问题 更多 >