Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find latitude and longitude using C#

I have a WCF service in C#.

In the Service call client sends a city name. I want to convert the city name to latitudes and longitudes and store in Database under demographics.

I am planning to use Google API to implement above functionality.

I have obtained an API key from Google and its of type 'Service account'.

How can I obtain the latitude and longitude using which APIs?

Do I need to install some SDK or any REST Service will do?

like image 666
MaxRecursion Avatar asked Jan 16 '13 09:01

MaxRecursion


People also ask

How do you find the distance between two latitude and longitude in C?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin... And for the sake of completeness: Haversine on Wikipedia.

How do I find the nearest location using latitude and longitude in C#?

Format("POINT({0} {1})", longitude, latitude)); var nearbyLocations = (from location in _context. Locations where // (Additional filtering criteria here...) select new { LocationID = location.ID, Address1 = location. Address1, City = location. City, State = location.

How do you calculate longitude?

The Earth rotates one full turn (360º of longitude) in one day. It therefore turns one degree of longitude in 1/360th of a day, or every four minutes. To calculate your longitude, you therefore simply need to work out the time difference between noon at your location and noon at the Prime Meridian.


1 Answers

You could try the NuGet package GoogleMaps.LocationServices, or just spin of its source code. It uses Google's REST API to get lat/long for a given address and vice versa, without the need for an API key.

You use it like this:

public static void Main() {     var address = "Stavanger, Norway";      var locationService = new GoogleLocationService();     var point = locationService.GetLatLongFromAddress(address);      var latitude = point.Latitude;     var longitude = point.Longitude;      // Save lat/long values to DB... } 
like image 116
khellang Avatar answered Oct 03 '22 07:10

khellang