How to add geocoding to Gizmox's GoogleMap
Categories: Custom Controls, Skinnable and Inherited Controls
|
Tags: Architects, Developers, C#, JavaScript, 2. Intermediate, 3. Advanced, Customization, Pre v6.3, v6.3
Revision:
1
Posted:
03/Jan/2009
Updated:
03/Jan/2009
Status:
Publish
Types: Article
|
There are two ways to use Geocoding with GoogleMap API : 1. By using the GClientGeoCoder Class from the API 2. 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 :
/// <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
About the author
Related Articles
|
Custom Controls
|
|
|
I played a bit around with the new 6.4 ListView control, its quite amazing what you can do with it. It opens a lot of new ways to present data in a better and more userfriendly way.
Tags:
Architects, Developers, Data Binding, C#, 2. Intermediate, 3. Advanced, Customization, Data Binding, v6.4 and Later
|
|
|
In this How to we are going to learn the basic usage of Visual WebGui RIA Platform Theme & Control designer.
Tags:
Developers, Graphic Designers, Theme, 1. Beginner, 2. Intermediate, Customization, v6.4 and Later, 3. Advanced
|
|
|
Tags:
Developers, Events, JavaScript, 1. Beginner, 2. Intermediate, Integration, Pre v6.3, v6.3, 3. Advanced
|
|
|
Tags:
Developers, Events, JavaScript, 2. Intermediate, 3. Advanced, Integration, Pre v6.3, v6.3
|
|
|
Tags:
Developers, Graphic Designers, Theme, 1. Beginner, 2. Intermediate, Customization, v6.4 and Later, 3. Advanced
|
|
|
Tags:
Developers, Events, JavaScript, 3. Advanced, Customization, v6.3
|
|
|
|