Anonymous user  |  Log in  |  Create Account

inverse Geocoding

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

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 response.

And after doing the initialization, we will call an inverseGeoCodeRequest, which will return a generic listing of instances of the class inverseGeocoding. It's ordered, more or less, by distance to the specified point. The class inverseGeocoding consists of the following properties:

  • name: name of the place closest to the specified point.
  • countryCode: ISO code of the country of the specified point.
  • countryName: name of the country of the specified point.
  • initialPoint: the point that we have specified.
  • nearestPlacePoint: coordinates of the place inhabited closest to the point than we have specified.
  • initialPointElevation: altitude above sea level (in meters) of the specified point.
  • nearestPlaceElevation: altitude above sea level (in meters) of the inhabited place closest to the point other than we what have specified.
  • distance: distance in Kilometers between the specified point and the closest inhabited place.

The next example 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, the result that gives us the inverse geoCoding is processed, and shown 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 showing Infowindow while another one comes from the server, making it look like we are waiting for the answer.

Code.aspx
<cc1:GMap ID="GMap1" runat="server" enableServerEvents="True" OnClick="GMap1_Click" />
Code.aspx.cs
private void BuildMap()
{
    GMap1.Add(new GControl(GControl.preBuilt.LargeMapControl));
    GMap1.enableHookMouseWheelToZoom = true;

    GLatLng latlng = new GLatLng("point");
    GInfoWindow window = new GInfoWindow(latlng, "<div style=\"height:140px;\"><blink>Loading...</blink></div>");

    StringBuilder sb = new StringBuilder();
    sb.Append("function(marker, point) {");
    sb.Append(window.ToString(GMap1.GMap_Id));
    sb.Append("}");

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

    GLatLng point = new GLatLng("point");
    GMove move = new GMove(1, point);
    GMarker marker = new GMarker(point);

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

    GMap1.Add(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>");
        sb.Append("<b>Nearest Place </b><br/>");
        //sb.AppendFormat("<a href=\"javascript:{0}\">goTo</a>", new GMove(10,geo.nearestPlacePoint).ToString(e.map));
        sb.AppendFormat("Place name: <i>{0}</i><br/>", geo.name);
        sb.AppendFormat("Point: <i>{0}</i><br/>", geo.nearestPlacePoint);
        sb.AppendFormat("Elevation: <i>{0}</i><br/>", geo.nearestPlaceElevation > -9000 ? geo.nearestPlaceElevation.ToString() : "No info");
        sb.AppendFormat("Country Name (Code): <i>{0} ({1})</i><br/>", geo.countryName, geo.countryCode);
        sb.AppendFormat("Click point - Nearest Place distance (Km): <i>{0}</i>", Math.Round(geo.distance, 3));
        sb.Append("</div>");
        sb.Append("<div>");
        sb.Append("<b>Click point</b><br/>");
        sb.AppendFormat("Point: <i>{0}</i><br/>", geo.initialPoint);
        sb.AppendFormat("Elevation: <i>{0}</i><br/>", geo.initialPointElevation > -9000 ? geo.initialPointElevation.ToString() : "No info");
        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