You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Google Places Nearby Search API无法按距离排序结果

Fixing Nearby Search Not Sorting by Distance in Google Places API

Let me break down why your request isn't returning results sorted by distance, even after removing the radius parameter:

Key Rules for rankBy=distance

Google's Places Nearby Search has strict requirements when using rankBy=distance that you might have overlooked:

  • You CANNOT use radius alongside rankBy=distance
    Even if you tried removing it later, double-check that your final request doesn't include both parameters. Having both will override the distance ranking behavior—this is a hard restriction in the API spec.

  • Ensure only one ranking parameter is present
    Make sure you're not accidentally including other ranking-related parameters (like rankBy=prominence somewhere in the request). The API will default to prominence if there's a conflict.

  • Verify your parameter formatting
    Your keyword=k rauta has a space—encode it properly as keyword=k%20rauta in the URL. While some clients handle spaces automatically, unencoded spaces can cause unexpected parsing issues that break the ranking logic.

Step-by-Step Fix

  1. Construct a valid request URL that adheres to the rules:
    https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=60.16915,24.9293937&rankBy=distance&keyword=k%20rauta&key=YOUR_API_KEY
    
  2. Clear any cached responses—sometimes old, incorrectly ranked results might be stored by your client or proxy.
  3. Check the API response for errors
    Look for an error_message field in the JSON response. If the API is rejecting your request for any reason (like invalid parameters), it won't apply the correct sorting.

Why This Works

When rankBy=distance is used correctly, the API will return results sorted strictly by ascending distance from your specified location coordinate. Any deviation usually comes from violating the parameter constraints mentioned above.

内容的提问来源于stack exchange,提问作者Mizlul

火山引擎 最新活动