Help us translate this website and improve this translation and earn free licenses!
Anonymous user  |  Log in  |  Create Account

How to...

inverse Geocoding

NOTE: Inverse geocoding uses the web services of geonames.org, under license Creative Commons. We recommended this page highly, because it has several related Web services to the geolocalization.

Up to now, we've seen how to obtain a place by its name, but what if we want a place's data by geographical coordinates?

For that we have inverse GeoCoding, and it's simple to use. There's nothing more to do than instantiate the class inverseGeocodingManager, which consists of four parameters:
  • point: the point on which we want information.
  • radius: radius in kilometers to look for near places.
  • N: maximum amount of places close by that we want in the result.
  • language: language in which we want the answer.
And after doing the initialization, we will call an inverseGeoCodeRequest, which will return a generic listing of instances of the class inverseGeocoding . It is ordered, more to less, near the point that we have indicated. The class inverseGeocoding consists of the following properties of interest:
  • name: name of the place closest to the indicated point.
  • countryCode: ISO code of the country of the indicated point.
  • countryName: name of the country of the indicated point.
  • initialPoint: the point that we have indicated.
  • nearestPlacePoint: coordinates of the place inhabited closest to the point than we have indicated.
  • initialPointElevation: altitude above sea level (in meters) of the indicated point.
  • nearestPlaceElevation: altitude above sea level (in meters) of the place inhabited closest to the point other than we what have indicated.
  • distance: distance in Kilometers between the indicated point and the closest inhabited place .
The next example shown is a typical example of the use of inverse geoCoding. When one clicks on a point of the map, it executes a server event. By handling it, we collect the result that gives us the inverse geoCoding, and we show it in an infoWindow. Additionally, we qualified the function "goTo" by adding a CustomJavascript that will take us closest to the place, and in addition we use the "trick" of sending one infowindow while another one comes from the server, making it seem that we are waiting for the answer.



Code.aspx
<cc1:GMap ID="GMap1" runat="server" enableServerEvents="true" OnClick="GMap1_Click" />
Code.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       this.arreglarMapa();
   }
}

private void arreglarMapa()
{
   GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));
   GMap1.enableHookMouseWheelToZoom = true;

   StringBuilder sb = new StringBuilder();
   sb.Append("function(marker, point) {");
   GLatLng latlng = new GLatLng("point");
   GInfoWindow window = new GInfoWindow(latlng, "<div style=\"height:140px;\"><blink>Loading...</blink></div>");
   sb.Append(window.ToString(GMap1.GMap_Id));
   sb.Append("}");

   GMap1.addListener(new GListener(GMap1.GMap_Id, GListener.Event.click, sb.ToString()));

   StringBuilder sb2 = new StringBuilder();
   sb2.Append("function goTo(point){");
   GLatLng point = new GLatLng("point");
   sb2.AppendFormat("{0}.setZoom(11);", GMap1.GMap_Id);
   GMove move = new GMove(1, point);
   sb2.Append(move.ToString(GMap1.GMap_Id));
   GMarker marker = new GMarker(point);
   sb2.Append(marker.ToString(GMap1.GMap_Id));
   sb2.Append("}");
   GMap1.addCustomJavascript(sb2.ToString());
}

protected string GMap1_Click(object s, GAjaxServerEventArgs e)
{
   inverseGeocodingManager igeoManager = new inverseGeocodingManager(e.point, "es");
   inverseGeocoding iGeos = igeoManager.inverseGeoCodeRequest();
   geoName geo;
   if (iGeos.geonames.Count > 0)
   {
       geo = iGeos.geonames[0];

       StringBuilder sb = new StringBuilder();
       sb.Append("<div align=\"left\">");
       sb.Append(""<b>Nearest Place "</b>");
       sb.Append("<br />");
       sb.AppendFormat("Place name: "<i>{0}"</i> ", geo.name);
       sb.Append("<br />");
       sb.AppendFormat("Point: "<i>{0}"</i>", geo.nearestPlacePoint.ToString());
       sb.Append("<br />");
       sb.AppendFormat("Elevation: "<i>{0}"</i>", geo.nearestPlaceElevation > -9000 ? geo.nearestPlaceElevation.ToString() : "No info");
       sb.Append("<br />");
       sb.AppendFormat("Country Name (Code): "<i>{0} ({1})"</i>", geo.countryName, geo.countryCode);
       sb.Append("<br />");
       sb.AppendFormat("Click point - Nearest Place distance (Km): "<i>{0}"</i>", Math.Round(geo.distance, 3));
       sb.Append("</div>");

       sb.Append("<br />");
       sb.Append("<div align=\"left\">");
       sb.Append(""<b>Click point"</b>");
       sb.Append("<br />");
       sb.AppendFormat("Point: "<i>{0}"</i>", geo.initialPoint.ToString());
       sb.Append("<br />");
       sb.AppendFormat("Elevation: "<i>{0}"</i>", geo.initialPointElevation > -9000 ? geo.initialPointElevation.ToString() : "No info");
       sb.Append("<br />");
       sb.Append("</div>");

       GInfoWindow window = new GInfoWindow(e.point, sb.ToString(), true);
       return window.ToString(e.map);
   }
   else return string.Empty;
}
Powered by Subgurim.NET and Comunactivo