4

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?

8

2 Answers 2

4

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...

1
2

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/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy