Vivian River
Vivian River

Reputation: 32370

How to specify bounds when calling Google Geolocation from C# code?

First of all, I am aware that the terms of licensing for Google geolocation prohibit using the service without showing the locations returned on a Google map.

That being said, I'm working on an application that uses Google geolocation. I'm constructing requests to Google geolocation in C#.

On the system I'm working on, bulk records containing addresses are sent off to another system for processing and then sent back. I need to know the latitude and longitude of all these addresses, and there are thousands of them. The thing is, this process may be repeated with only a few addresses actually changed. I need to compare these with my local records to see which ones have changed. I can't do this in Javascript, so it has to be done server-side, in C# code.

(I do realize that this could apply to any server-side language, but I'm using C#.)

The request goes to http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false. This gets me the latitude and longitude, but I need to restrict the results to a rectangular area on the map.

In a previous version of my application before I had this requirement, I did do this in Javascript as Google directs in their documentation using the Javascript google.maps.LatLng object.

One thing that I tried that didn't work was I tried to use Telerik Fiddler to peer into the request sent to Google by the Javascript code. For some reason, this request seems to be invisible to Fiddler. (Why? I have no idea.)

Another thing I tried was trying to filter the results on my server. This doesn't work, either, for a subtle reason. Some results are returned only when biased toward their region. For example, if you pass in "Honolulu", you will get back the latitude and longitude for Honolulu, Hawaii, USA, as you would expect. However, there is also a small town in North Carolina called Honolulu. If the user had an expectation that he is searching in North Carolina, he might not be able to find this town without specifying the bounds because Google will always return Honolulu, Hawaii when the search is performed without bounds.

TL;DR; When manually constructing a web request in a server-side language like C#, how do I tell Google geolocation to search within the bounds of a rectangular square on the map?

Upvotes: 0

Views: 454

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117314

The format of the bounds-parameter is

sw-lat,sw-lng|ne-lat,ne-lng

so for Honolulu,NC it would be e.g.

bounds=35.3538404,-77.2976329|35.3720392,-77.2656181

The problem: the bounds-parameter doesn't restrict the results(see: https://developers.google.com/maps/documentation/geocoding/#Viewports)

Result(still Hawaii): http://maps.googleapis.com/maps/api/geocode/xml?address=honolulu&bounds=35.3538404,-77.2976329|35.3720392,-77.2656181

What you can do: when you e.g. know the expected state, use component-filtering. For Honolulu,NC the parameter would be

 components=administrative_area:NC 

Result(in North Carolina): http://maps.googleapis.com/maps/api/geocode/xml?address=honolulu&components=administrative_area:NC

Upvotes: 1

Related Questions