4
votes

I got a error when i get lat and long using locationService.GetLatLongFromAddress

The error is :

Sequence contains no elements

I have tried this code

var locationService = new GoogleLocationService();
var points = locationService.GetLatLongFromAddress("Ram Theatre Bus Stop, Arcot Road, Vadapalani, Chennai, Tamil Nadu");
mapDetail.Latitude = points.Latitude;
mapDetail.Longitude = points.Longitude;
mapDetail.CollegeAddressId = addressDetail[i].CollegeAddressId;

What is the problem? How can i solve this?

2
Could you please post the code of the GoogleLocationService()? thanks - Christos
GoogleLocationService is a service class name from GoogleMaps.LocationServices dll - user3541102
simple bug; I've submitted a pull request: github.com/sethwebster/GoogleMaps.LocationServices/pull/6 - Marc Gravell
@RameshRajendran the library author has merged the pull request pushed the update; try updating - Marc Gravell

2 Answers

4
votes

You would typically get that if code uses .First() or .Single() on a sequence (IEnumerable<T>) that has (as the message suggests): no elements. Meaning: an empty sequence (not to be confused with a null sequence). You don't show code that does that, so I can only assume this happens inside .GetLatLongFromAddress(). So it sounds like there is a bug, probably relating to the "not found" case, but in code that we can't see. Personally, I would expect the "not found" case to return a null, or to throw some explicit "not found" exception. If this bug is inside a library: tell the library authors about it. Or better: fix it, and submit a pull request (if you can).

Edit: here we go:

XDocument doc = XDocument.Load(string.Format(APIUrlLatLongFromAddress,
    Uri.EscapeDataString(address)));
var els = doc.Descendants("result").Descendants("geometry")
    .Descendants("location").First();
if (null != els) {...}

IMO, that should be:

XDocument doc = XDocument.Load(string.Format(APIUrlLatLongFromAddress,
    Uri.EscapeDataString(address)));
var els = doc.Descendants("result").Descendants("geometry")
    .Descendants("location").FirstOrDefault();
if (null != els) {...}

One line code fix to send them...

2
votes

I've merged in Mark Gravell's pull request for the GoogleLocationService and have pushed an updated Nuget package.

https://www.nuget.org/packages/GoogleMaps.LocationServices/