iOS Swift中经纬度转地址遇Google Geocode请求被拒问题
Hey there! I’ve run into this exact issue before, so let’s walk through the most likely fixes for your denied requests:
1. Double-Check Your API Key’s IP Restrictions
Since you modified settings in the Developer Console, this is the first place to look:
- Head to your Google Cloud Console, navigate to APIs & Services > Credentials, and find the API key you’re using.
- Look at the IP address restrictions section:
- If you selected "Restrict key to specific IP addresses", make sure your device’s public IP address is listed here (you can find your public IP by searching "what is my IP" in your browser).
- For testing, temporarily switch to "None" (no restrictions) to see if the request works—just remember to re-enable restrictions later for security.
2. Verify the Geocoding API is Still Enabled
It’s easy to accidentally disable an API when tweaking settings:
- Go to APIs & Services > Library in the Console.
- Search for "Geocoding API" and confirm it shows "Enabled". If not, click the "Enable" button to turn it back on.
3. Check for API Key Format Errors
Looking at your request URL, I noticed you have quotes around your API key: key="MyAPI Key"
Google’s API doesn’t accept quotes here—remove them so your key parameter looks like key=YourActualAPIKey. Those extra quotes are probably being treated as part of the key itself, which will definitely cause an authorization failure.
4. Confirm the Key is Linked to the Correct Project
Make sure the API key you’re using is associated with the same project where you enabled the Geocoding API. Sometimes when managing multiple projects, it’s easy to mix up keys across different projects.
Test with a Simple Curl Command
To debug quickly, run this command in your terminal (replace YourActualAPIKey with your real key):
curl "https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9892037613554,80.2505832381126&key=YourActualAPIKey"
This will show you the exact error details, which can help narrow down the issue if the above steps don’t fix it.
内容的提问来源于stack exchange,提问作者Hari Narayanan

