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...
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.
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; } }
}
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With