问题
Results of url https://maps.googleapis.com/maps/api/geocode/json?address=75019&sensor=false&components=postal_code:75019|country:US is different from results of url https://maps.googleapis.com/maps/api/geocode/json?address=75019&sensor=false&components%3Dpostal_code:75019|country:US
The only difference between the two url's is that the "=" character after "components" is url encoded in the second url. As a result in second url: I get 2 results back, one for Paris & one for Texas, which is undesirable. I want results just for country = US. We are using the second url in production, but would like the results that we get from the first url.
Issue is that even though the filter clearly mentions the country as US, why am I getting an address from France?
Any solution or insights? Is the diagnosis correct?
回答1:
You're not specifying the country as US in the second URL:
The second url gives google a parameter named "components%3Dpostal_code:75019|country:US" with no content, while the first one gives google a parameter named "components" with a content of "postal_code:75019|country:US" - the = sign is important as the separator between parameter name and value. If you url-encode it, it is not considered for this purpose anymore.
So, you should not URL-encode the equals sign if it should signify the separation between a parameter name and its value... meaning you will have to change your production code to produce the first URL. The second one just isn't representing what you want to express.
Since you don't use any other url-encoded characters (at least not in the link shown above), it might also work to do a separate Url-decoding step somewhere before sending the request to google (if you absolutely can't change production code).
来源:https://stackoverflow.com/questions/18120415/google-geo-coding-returns-different-results-if-url-is-encoded-versus-if-it-is-no