Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a place name based on coordinates?

Given the coordinates of a location, is there a way to get the place name?

I don't mean the address, I mean the "title" of the place, e.g.:

Coordinates: 76.07, -62.0 // or whatever they are
Address: 157 Riverside Avenue, Champaign, Illinois
Place name: REO Speedwagon's Rehearsal Spot

-or:

Coordinates: 76.07, -62.0 // or whatever they are
Address: 77 Sunset Strip, Hollywood, CA
Place name: Famous Amos Cookies

So is there a reverse geocoding web service or something that I can call, a la:

string placeName = GetPlaceNameForCoordinates(76.07, -62.0)

...that will return "Wal*Mart" or "Columbia Jr. College" or whatever is appropriate?

I've found references to other languages, such as java and ios (Objective C, I guess), but nothing specifically for how to do it in C# from a Windows store app...

UPDATE

I already have this for getting the address (adapted from Freeman's "Metro Revealed: Building Windows 8 apps with XAML and C#" page 75):

public static async Task<string> GetStreetAddressForCoordinates(double latitude, double longitude)
{
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("http://nominatim.openstreetmap.org");
    HttpResponseMessage httpResult = await httpClient.GetAsync(
        String.Format("reverse?format=json&lat={0}&lon={1}", latitude, longitude));

    JsonObject jsonObject = JsonObject.Parse(await httpResult.Content.ReadAsStringAsync());

    return string.format("{0} {1}", jsonObject.GetNamedObject("address").GetNamedString("house"),
                                    jsonObject.GetNamedObject("address").GetNamedString("road"));
 }

...but I see nothing for Place Name in their docs; they seem to supply house, road, village, town, city, county, postcode, and country, but no Place Name.

like image 979
B. Clay Shannon-B. Crow Raven Avatar asked Feb 18 '23 07:02

B. Clay Shannon-B. Crow Raven


2 Answers

I usually store the latitude/longitude coordinates and then use GMaps to look up the location, then "on a best effort basis" - look up the place's name using the address - again via Google Maps.

static string baseUri = 
  "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
string location = string.Empty;

public static void RetrieveFormatedAddress(string lat, string lng)
{
    string requestUri = string.Format(baseUri, lat, lng);

    using (WebClient wc = new WebClient())
    {
        string result = wc.DownloadString(requestUri);
        var xmlElm = XElement.Parse(result);
        var status = (from elm in xmlElm.Descendants() where 
            elm.Name == "status" select elm).FirstOrDefault();
        if (status.Value.ToLower() == "ok")
        {
            var res = (from elm in xmlElm.Descendants() where 
                elm.Name == "formatted_address" select elm).FirstOrDefault();
            requestUri = res.Value;
        }
    }
}

Edit:

Here's a simple version of the reverse:

public static Coordinate GetCoordinates(string region)
{
    using (var client = new WebClient())
    {

        string uri = "http://maps.google.com/maps/geo?q='" + region + 
          "'&output=csv&key=sadfwet56346tyeryhretu6434tertertreyeryeryE1";

        string[] geocodeInfo = client.DownloadString(uri).Split(',');

        return new Coordinate(Convert.ToDouble(geocodeInfo[2]), 
                   Convert.ToDouble(geocodeInfo[3]));
    }
}

public struct Coordinate
{
    private double lat;
    private double lng;

    public Coordinate(double latitude, double longitude)
    {
        lat = latitude;
        lng = longitude;

    }

    public double Latitude { get { return lat; } set { lat = value; } }
    public double Longitude { get { return lng; } set { lng = value; } }

}
like image 125
Jeremy Thompson Avatar answered Feb 21 '23 01:02

Jeremy Thompson


I've found references to other languages, such as java and ios (Objective C, I guess)

Look closely into those references - most of them are likely to use reverse geocoding web services... and those could be used by your Windows Store app as well. Pick a service which has appropriate features and restrictions for your app, and make HTTP requests to it. (You may even find there's an appropriate client library available, although I guess that's relatively unlikely right now, due to the recent-ness of Windows 8...)

like image 34
Jon Skeet Avatar answered Feb 21 '23 01:02

Jon Skeet