简体   繁体   中英

Error Using GoogleMaps.LocationServices to Find Latitude and Longitude in C#

I am looking for advice on how to get the latitude and longitude of an address using C#. I do not know how to interpret the error message I receive.

Here is my code:

using GoogleMaps.LocationServices;

var address = "Dallas, Texas";
var locationService = new GoogleLocationService();
var point = locationService.GetLatLongFromAddress(address);
var latitude = point.Latitude;
var longitude = point.Longitude;

Here is the error:

Exception Unhandled: System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 403 (Forbidden).'

I get it on line 5, var point = locationService.GetLatLongFromAddress(address);

For your reference, this is the GoogleMaps.LocationServices assembly .

Thank you so much for your help.

You're not identifying your API key in your location service constructor...so your request is forbidden.

var locationService = new GoogleLocationService(apiKey: "YOUR_API_KEY");

See https://github.com/sethwebster/GoogleMaps.LocationServices and https://developers.google.com/maps/documentation/javascript/get-api-key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM