2

I am trying to get coordinates stored in the address model. I am using google maps api with the Chadly/Geocoding.net to get the coordinates but it requires async and I have never used async within a model and having problems getting it to work.

It is throwing me this error

"Cannot implicitly convert type 'Geocoding.Location' to 'System.Threading.Tasks.Task<Geocoding.Location>'"

Anything special I have to do to get this to work?

   public string FullAddress 
    {
        get 
        { 
            return Address + " " + City + " " + State + "" + ZipCode;
        }
    }
    public async Task<Geocoding.Location> Coordinates
    {
        get
        {
            IEnumerable<Address> addresses = await geocoder.GeocodeAsync(FullAddress);

            return addresses.First().Coordinates;
        }

    }
Nkosi
  • 235,767
  • 35
  • 427
  • 472
dbollig
  • 81
  • 4
  • What are the problems? Any errors? – Nkosi Nov 25 '18 at 23:20
  • Hey Nkosi I updated with my error I am receiving. – dbollig Nov 25 '18 at 23:23
  • 1
    I suggest that you don't call async methods from a property getter. Make them a method. Also, please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) because we simply can not tell what you actually need just from the code that you provided – Dennis Kuypers Nov 25 '18 at 23:25
  • `Coordinates` should be a method. you can't have async properties – Nkosi Nov 25 '18 at 23:27

2 Answers2

2

Coordinates should be refactored to a method. You can't have async properties

public async Task<Geocoding.Location> GetCoordinates(){
    IEnumerable<Address> addresses = await geocoder.GeocodeAsync(FullAddress);
    return addresses.First().Coordinates;
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

unfortunately, you cannot use an async property getter in C#.

Just turn it to a method instead, and it will work fine.

public async Task<Geocoding.Location> GetCoordinates()
{
     IEnumerable<Address> addresses = await geocoder.GeocodeAsync(FullAddress);

     return addresses.First().Coordinates;
}
Pac0
  • 21,465
  • 8
  • 65
  • 74