Reverse Geocoding: returning single elements of the address

Nov 21, 2011 at 1:39 PM

Hi,

I'm looking to return specific elements from a reverse geocoding request rather than the full address string.

I can return a single element using something like 

response.Results[0].AddressComponents.ElementAt(1).ShortName

but I need to get specific type such as the country code, and these types have a different index depending on the country.

I'm a bit of a C# novice, and IEnumerables are new to me. I would be really grateful if someone could point me in the right direction with this.

Thanks

Marek

Coordinator
Nov 22, 2011 at 6:02 AM
Edited Nov 22, 2011 at 6:04 AM

Hi Marek,

You can try using LINQ.
If I get right your question, first you can return a particular address component from first result with something like:

var query = from c in response.Results[0].AddressComponents
            where c.Types.Contains(GeoAddressType.country) && c.ShortName == "MY_SHORT_NAME"
            select c;
var addressComponent = query.FirstOrDefault();

But if you are interested in a result of the request with particular country address component you can use something like:

var query  = from r in response.Results
             from c in r.AddressComponents
             where c.Types.Contains(GeoAddressType.country) && c.ShortName == "MY_SHORT_NAME"
             select r;
var result = query.FirstOrDefault();

It is obvious, but just to say it: remove MY_SHORT_NAME with the desired one.
Hope this helps.

Regards

Nov 22, 2011 at 5:57 PM

Hi Velio,

Thanks for your help, that’s given me exactly what I want!

One more thing. I noticed that you added some extra GeoAddressTypes which are not referenced in Google’s v3 documentation. I’ve found one more: bus_station. For an example, see the location at 53.94921875, -1.0517578125. Google Places and Google Transit seem to have quite a few different placemarks that can be picked up.

Is there an easy way to get around this without creating a new class library containing all possible places? My current workaround is to change the lat-long by a small amount until the location is accepted by the geocoder.

Regards

Marek

From: velio [email removed]
Sent: 22 November 2011 07:02
To: Shafer, Marek
Subject: Re: Reverse Geocoding: returning single elements of the address [googlemap:280263]

From: velio

Hi Marek,

You can try using LINQ.
If I get right your question, first you can return a particular address component from first result with something like:

var query = from c in response.Results[0].AddressComponents
            where c.Types.Contains(GeoAddressType.country) && c.ShortName == "Bulgaria"
            select c;
var addressComponent = query.FirstOrDefault();

But if you are interested in a result of the request with particular country address component you can use something like:

var query  = from r in response.Results
             from c in r.AddressComponents
             where c.Types.Contains(GeoAddressType.country) && c.ShortName == "Bulgaria"
             select r;
var result = query.FirstOrDefault();

Hope this helps.

Regards