Steve Trefethen
Contact me Send mail to the author(s)
About Me View my LinkedIn profile

Powered by discountASP.NET
referal ID: sdtref
Why recommend discountASP.NET?

Archives
Statistics
Total Posts: 524
This Year: 26
This Month: 0
This Week: 1
Comments: 1835
Disclaimer
The posts on this weblog are provided �AS IS� with no warranties, and confer no rights. The opinions expressed herein are my own personal opinions and do not represent my employer�s view in any way.
# Wednesday, June 06, 2007
« Google AJAX Search and Google AJAX Feed ... | Main | Yet another reason to upgrade my home ma... »

Using Google Maps from a Windows Client application

Tagged: Programming

Tags:
The other day I blogged about mashing up Google Maps and VCL in a Delphi Win32 application with support for bi-directional VCL-> JavaScript and JavaScript-> VCL allowing for rich interaction with the map. The first part of this "conversation" is actually very easy to accomplish. Here is what you need to do if you want to give it a try. I'll talk about this from the Delphi perspective though the same logic will work from C# or any other language that can host the Microsoft WebBrowser control.
  1. Start a new VCL application
  2. Drop a TWebBrowser control
  3. Create a Form1.OnCreate event handler
  4. Add the following line of code:
    WebBrowser1.Navigate(
      'http://www.stevetrefethen.com/files/googlemap.htm');
  5. Add a TButton to the form and an OnClick event handler and add the following code:
procedure TForm1.Button1Click(Sender: TObject); var Doc2: IHTMLDocument2; begin with WebBrowser1.Document as IHTMLDocument2 do with parentWindow do execScript('createMapMarker("31.05173494", "-122.03160858", "test")', 'JavaScript'); end;

This will create a new marker on the map in Scotts Valley, CA. In fact, you can execute any method of the Google Map object by replacing the first parameter in execScript (above) with something like this:

map.panTo(new GLatLng(37.05173492, -122.031608568));

Hopefully, you can see the possibilities this creates. To get it working on your own domain you'll need to do the following:

  1. Get yourself GMail account if you don't already have one
  2. Get a Google Maps API key you'll need a domain where you can upload files to
  3. Right click and save this file to disk
  4. Edit the file you just download and change the API to use your key (mine won't work on your domain)
  5. Change the .Navigate call to use your URL

That's it, you can now interact with Google maps from a Windows client application (VCL in this case). As you can see mashups aren't strictly the domain of web only applications. In a subsequent post I'll talk about how to interact with the hosting application from the JavaScript on the page for true two way interaction.

[UPDATE: April 2, 2008] Related posts:

||| |
Comments [9] # permalink Posted @ 4:16PM
Thursday, April 02, 2009 9:36:05 PM (Pacific Daylight Time, UTC-07:00)
hi steve,
It would be very helpfull if you add the unit "MSHTML" for IHTMLDocument2 to your document.

Thank you!
Rainer (Germany Heidelberg)
Rainer Unger
Thursday, April 02, 2009 9:55:02 PM (Pacific Daylight Time, UTC-07:00)
Hi Rainer,
I wrote this post shortly before leaving CodeGear and haven't worked in Delphi since. I know there are lots of references to IHtmlDocument2 on Google which I think is your best bet. You can also generate MSHTML.pas for yourself.
Friday, April 17, 2009 6:34:34 AM (Pacific Daylight Time, UTC-07:00)
Hi Steve,

It's nice. I tried it and it's working fine.

Thank you,
Adesh Agnihotri (UK)

Friday, April 17, 2009 2:52:00 PM (Pacific Daylight Time, UTC-07:00)
I downloaded your sample Delphi app, and it works great until I substitute my own Google key. I used the example html code that you provided for the hosting page, as well as changed the HTMLURL to that target page. I noticed that your html page sample has a script that refers to an ashx page that calls the load_map function. Do I need to do likewise--add a separate page for that? Thanks for the sample app, it's really interesting!
Thursday, April 30, 2009 5:43:59 AM (Pacific Daylight Time, UTC-07:00)
hi Steve,
This information is very helpful... thanx a tonn....
i have a problem though, i tried running ur script wid a c# windows application...
so i had to change the execScript func call to InvokeScript....
whn i try calling the createMapMarker, it gives an error "map is null or not an object".
Please let me know wat have i done wrong???
i need this for a prototype verion to show my manager that google maps is a good option for showing locations in my application

Waiting for ur response.
Thanks
Batul
Thursday, April 30, 2009 7:34:32 AM (Pacific Daylight Time, UTC-07:00)
Batul,
You'll just need to debug it. I haven't converted this script to C# and providing a description such as this isn't a means of debugging the problem. Btw, you really need to review Google's Terms of Service.
Thursday, April 30, 2009 8:20:47 AM (Pacific Daylight Time, UTC-07:00)
Thanks for ur response.... Is there anything very imp that i need to knw in that? i have read it a couple of times, but i dont understand wat i shud be doing for the proto version.... gettn a domain by the ofc name is goin to get difficult unless i prove the advantages of gmaps.....
Batul
Thursday, April 30, 2009 9:23:05 AM (Pacific Daylight Time, UTC-07:00)
Batul, please read this. Good luck.
Tuesday, May 19, 2009 7:48:42 AM (Pacific Daylight Time, UTC-07:00)
Please Get This Google Map to This email
Ehsan Panahi
Comments are closed.