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

Archives
Steve Trefethen Steve's RSS Feed Subscribe or via email
What's this?
Contact me Send mail to the author(s)
About Me
View my LinkedIn profile

Add to Google
Subscribe with Bloglines
MCP Microsoft Certified Professional

Falafel Software
ActiveFocus Project Management Solution by Falafel Software
Online or OnSite TestComplete Training
Blogroll
Recent Comments
My Online Tools
Stats
Total Posts: 460
This Year: 65
This Month: 1
This Week: 2
Comments: 1616
Tags
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.
My most popular blog posts (Q1 2008)
# 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

Posted @ 4:16PM by Steve Trefethen

Categories: 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:

||| |
Thursday, June 07, 2007 2:29:27 AM (Pacific Daylight Time, UTC-07:00)
Hey Steve,

Thanks for the tutorial.

In Portugal we say:
"Give a fish and the person will always come back, give a fishing cane and the person never will bother you again"
(roughly translated). That's the kind of tutorial I like, you gave me the fishing cane !!

This concludes we all asking for the code on your mashup post doesn't it ?

Cheers,
Gustavo Carreno
Thursday, June 07, 2007 3:04:27 AM (Pacific Daylight Time, UTC-07:00)
Hi Gustavo,
Thanks for the kind words I'm glad you found this useful. The part I haven't discussed is calling back into the VCL code from Javascript. That's pretty significant because it means you can put markup in the browser (links, buttons etc.) and have them execute VCL code. It's more involved to do in a generic fashion but very useful I think.
Thursday, June 07, 2007 5:56:55 PM (Pacific Daylight Time, UTC-07:00)
Hey Steve,

I've seen some of the callback code embedded on the FeedDemon html Newspaper and I really wanted to replicate that !!

I'll be, eagerly, waiting to read your next post about the callbacks !!

Cheers,
Gustavo Carreno
Friday, June 08, 2007 3:31:58 PM (Pacific Daylight Time, UTC-07:00)
Great idea Steve.

After a few hours of working I still can not get the marker to show up on my map. The map is just fine but when I click on the button... nothing!
Is it possible I am missing something. Here is the code I pasted:

procedure TForm1.Button_add_markerClick(Sender: TObject);
var
Doc2: IHTMLDocument2;
begin
with WebBrowser1.Document as IHTMLDocument2 do
with parentWindow do
execScript('createMapMarker("31.05173494","-122.03160858", "test")', 'JavaScript');

end;

John
Tuesday, June 26, 2007 3:39:56 AM (Pacific Daylight Time, UTC-07:00)
Doesn't work for me either (i mean map is Ok, but no pointers at all).
Lawful Citizen
Monday, July 16, 2007 5:09:55 PM (Pacific Daylight Time, UTC-07:00)
Idioma = Português
Olá

Estou utilizando o delphi7 e está dando a seguinte mensagem de erro:

Undeclared Identifier 'IHTMLDocument2'

var
Doc2: IHTMLDocument2;

Qual o prblema ?

José Ricardo
Monday, February 25, 2008 6:52:17 PM (Pacific Standard Time, UTC-08:00)
Jose Ricardo:
adicione nos seus USES: MSHTML
;)

ah e comigo tb nao aparece nenhum marcador :(
(translation: i get no map marker either :( )
PT
Monday, February 25, 2008 9:28:11 PM (Pacific Standard Time, UTC-08:00)
PT and others,
My first suggestion would be to ensure that you have the latest version of MSHTML. For more information about importing that type library refer to <a href="PT and others,
My first suggestion would be to ensure that you have the latest version of MSHTML. For more information about importing that type library refer to this post." title="Importing mshtml type library>this post</a>.

My second point would be that this post was written using the shipping build of Delphi at the time not Delphi 7 as is mentioned here. Delphi 7 is now many, many years old and lots has changed WRT the RTL, compiler and VCL. I did not attempt to test this code with earlier versions of Delphi.
Saturday, March 01, 2008 4:42:23 AM (Pacific Standard Time, UTC-08:00)
Hi Steven,

Congratulations!
I never thought I could do this, even more using Delphi 5.
I only had to change "external.Caption = 'Hello from Javascript';" from CreateMapMarker function to "marker.openInfoWindow(html);".
The only thing is when I tried to put and use "googlemap.ashx" from my page it didn't load.
Know nothing about "ashx". My page is a free domain from googlepages.
http://sorocabaemmapas.googlepages.com

Can I do anything?

Thanks.
Monday, March 03, 2008 9:07:04 AM (Pacific Standard Time, UTC-08:00)
Furlani,
Congrats! I'm glad you got it working. The issue with the .ashx file is that that's ASP.NET which is not supported on googlepages.com. You need to either change that page accordingly or host it from a provider that supports ASP.NET.
Tuesday, March 04, 2008 4:56:14 AM (Pacific Standard Time, UTC-08:00)
Hey Steve,
Thank you for your reply.
I'll think later about another way to load map without using .ashx as for now I'm focused on implementing some new functions on googlemap.htm file. As soon as I have something consistent I'll tell you.
Now I want to invite you to visit the page, using Google Maps API, I created for the city, Sorocaba, I was born and live. As my language is portuguese (Brazil) I'll try to help you understand.
The site presents several thematic maps (as for public state schools, university schools, public sports buildings, hospitals and healthy care, government places, etc). If you go to the first link called "Meu Local" ("My Place") on main page, you can define and "Gravar" ("Save") a place that will be used as the Start point for a "Trajeto" ("route") on all thematic maps.
The cool thing is that all thematic maps are loaded from the same HTML. Map title, theme field and legend items are obtained, together with data for the markers, from a XML passe as parameter.
The last link on main page is my last work, a moving marker that I used to ilustrate the 4 different circuits stablished for a bike meeting that occurred last sunday (03/02) here in Sorocaba.
Click Here for my page

Cheers
Furlani

Tuesday, March 04, 2008 6:41:05 AM (Pacific Standard Time, UTC-08:00)
Hey Steve,

this tutorial is extremely interesting!!
i am trying to do a similar thing in C++: basically i've created a 3D reproduction of my college and i would like to show the position of the user on an external map (google maps would be brilliant!!!!).
the only problem is that i don't have a clue how to do it!
any chance i can use your tutorial in C++? would the code and the procedure be different?

any help would be more than appreciated! :)

Francesco
Francesco
Tuesday, March 04, 2008 10:26:42 AM (Pacific Standard Time, UTC-08:00)
Furlani,
Very cool! I really enjoy getting comments yours thanks, it makes blogging worthwhile!
Friday, March 28, 2008 7:58:21 AM (Pacific Daylight Time, UTC-07:00)
Hi Steve,

I'm back!
I implemented several functions on google.htm code and successfully got rid of .ashx.
I've created a Delphi project too and you can download it's source HERE to try the new functions.

Bye.
Wednesday, April 02, 2008 7:15:20 AM (Pacific Daylight Time, UTC-07:00)
Olá...

Alguém já fez o caminho inverso, ou seja, o usuário clicar num marcador no mapa e o mapa enviar alguma informaçao para a aplicação que o abriu (feita em Delphi, no meu caso) ou essa aplicação 'capturar' alguma informação do mapa e localizar um registro num Dataset???

Abraços
Jair
Jair
Wednesday, April 02, 2008 8:23:48 AM (Pacific Daylight Time, UTC-07:00)
Jair,
Be sure to check out this example of two way communication between the browser and the VCL.
Saturday, May 17, 2008 11:02:19 AM (Pacific Daylight Time, UTC-07:00)
Olá Furlani eu vi que você desenvolveu uma pequena aplicação em Delhi para a API do Google Maps, estou tenando fazer a mesma coisa você pode me disponibilizar o código fonte dela para ler e aprender como faz?

Desde já agradeço, se puder envie para o meu e-mail: alexandre@brim.com.br

Obrigado.
Saturday, May 17, 2008 11:04:50 AM (Pacific Daylight Time, UTC-07:00)
Hi Steve you can send me the full source code of your examples, thanks...

Best regards...

Please send to alexandre@brim.com.br
Saturday, May 17, 2008 2:54:13 PM (Pacific Daylight Time, UTC-07:00)
Hi Alexandre,
There isn't any additional source code involved here other than the D2007 RTL/VCL and what you see in the single ButtonClick event above. If you're using a version prior to D2007 it's possible you may have problems. As I'm no longer working in Delphi I won't be revisiting these examples. I'd suggest looking at Marco Cantu's blog as well as he has done some similar things.

Good luck.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview