How to add geocoding to Gizmox's GoogleMap
March 12, 2009 :: 945 Views :: User Rating:

There are two ways to use Geocoding with GoogleMap API :
- By using the GClientGeoCoder Class from the API
- By using a Geocoding Request (manually sending a request to http://maps.google.com/maps/geo? with requested parameters.)
Note : You can find information about :
- The first method here : http://code.google.com/intl/fr-CA/apis/maps/documentation/services.html#Geocoding
- The second method here : http://code.google.com/intl/fr-CA/apis/maps/documentation/geocoding/index.html
Because of our project’s time limits, I used the first method. This implies that the GoogleMap object must be added to your controls’ collection even if you don't need it and it is hidden.
Here we go.
The first thing to do is to modify the javascript files so that they will be able to call the api's geocoding functions.
Here's an example of what I did :
In file "GoogleMap.js", I added this function :
/// <method name="GoogleMap_GetLatLng">
/// <summary>
/// This methods call the GetLatLng method of our GMap object
/// </summary>
/// <param name="strGuid">The component id.</param>
/// <param name="strAddress">Searched address</param>
function GoogleMap_GetLatLng(strGuid, strAddress)
{
// Get IFrame contained with in element
var objIFrame = Web_GetTargetElementByDataId(strGuid);
if(objIFrame)
{
// Check that iframe method is found
if(objIFrame.contentWindow.GoogleMap_GetLatLng)
{
objIFrame.contentWindow.GoogleMap_GetLatLng(strGuid, strAddress);
}
}
}
In file "GoogleMapFrame.js", I added this function :
function GoogleMap_GetLatLng(strGuid, strAddress)
{
var geocoder = new GClientGeocoder();
geocoder.getLatLng(strAddress,
function (point) {
var objClickEvent = mobjApp.Events_CreateEvent(mstrControlId,"ReceivedLatLng", null, true);
mobjApp.Events_SetEventAttribute(objClickEvent,"LatLng",point);
mobjApp.Events_RaiseEvents();
}
);
}
Now, we must add some members to our "GoogleMap.cs" file to handle this event. We need :
- An event handler
- Some members to handler Latitude, Longitude and zoom level.
Here's a sample of the code I added :
/// <summary>
/// Occurs when [api received requested lat/long].
/// if ReceivedLatLng == null, an error has happenend
/// </summary>
public event EventHandler LatLngReceived;
private GLatLng mLastReceivedLatLng;
private int mintZoomLevel = -1;
Don't forget to add properties that will enable you to read this data outside the box. You must also add the event handler :
protected void OnLatLngReceived(EventArgs objArgs)
{
if (LatLngReceived != null)
LatLngReceived(this, objArgs);
}
Now that everything is ready, you need to handle the javascript event. To do this, you must add a case to the "FireEvent" method :
protected override void FireEvent(IEvent objEvent)
{
// If is a member event
if (!string.IsNullOrEmpty(objEvent.Member))
{
// ... Existing code ...
}
else
{
base.FireEvent(objEvent);
switch (objEvent.Type)
{
case "PositionChanged":
mintLongitude = Double.Parse(objEvent["Longitude"], CultureInfo.InvariantCulture);
mintLatitude = Double.Parse(objEvent["Latitude"], CultureInfo.InvariantCulture);
OnPositionChanged(EventArgs.Empty);
break;
case "ZoomLevelChanged":
mintZoomLevel = Int32.Parse(objEvent["ZoomLevel"], CultureInfo.InvariantCulture);
OnZoomLevelChanged(EventArgs.Empty);
break;
case "ReceivedLatLng":
if (objEvent["LatLng"] != null && objEvent["LatLng"].ToString() != String.Empty)
{
try
{
string coordAsString = objEvent["LatLng"].ToString().Replace("(", "").Replace(")", "");
string[] xy = coordAsString.Split(',');
mLastReceivedLatLng = new GLatLng(double.Parse(xy[0], CultureInfo.InvariantCulture), double.Parse(xy[1], CultureInfo.InvariantCulture));
}
catch(Exception e)
{
mLastReceivedLatLng = null;
}
}
else
mLastReceivedLatLng = null;
OnLatLngReceived(EventArgs.Empty);
break;
}
}
}
Now you can handle the return of a call to geocoding, but how do you call it?!
You need to add a simple method to the "GoogleMap.cs" file :
public void FindLatLng(string address)
{
InvokeMethodWithId("GoogleMap_GetLatLng", address);
}
Now you have a geocoding functionnality. Here's an example:
public MainForm2()
{
InitializeComponent();
googleMap1.LatLngReceived += new EventHandler(googleMap1_LatLngReceived);
}
private void button1_Click(object sender, EventArgs e)
{
googleMap1.FindLatLng("1600 Amphitheatre Parkway Mountain View, CA 94043");
}
void googleMap1_LatLngReceived(object sender, EventArgs e)
{
if (googleMap1.ReceivedLatLng != null)
MessageBox.Show(googleMap1.ReceivedLatLng.Latitude.ToString() + ", " + googleMap1.ReceivedLatLng.Longitude.ToString());
}
Feel free to leave any comments or questions. If someone is willing to write a detailed how-to on the second method, it would be nice
Thanks
Francois