Delphi Handbooks Collection


Delphi XE Handbook


Delphi 2010 Handbook


June 18, 2007

Google Maps with a Delphi Server

Beside using a Google Map inside a Delphi application, you can write a custom server hosting maps, providing dynamic data to it.

Steve Trefethen has been playing with Google Maps scripted by a Delphi VCL client application hosting Internet Explorer using a TWebBrowser control, and blogged on Mashup of Google Maps and VCL and Using Google Maps from a Windows Client Application. Allen Bauer added to the topic a long and detailed post on Adding Active Scripting to your Delphi Win32 application and a second one on Connecting an event to a Active Script function. In short, you can run JavaScript from your Delphi application, and hook it not only to Internet Explorer pages, but to the VCL in general.

Beside using a Google Map inside a Delphi application, you can write a custom server hosting maps, providing dynamic data to it. This is what I did last year and repeated (with a nicer demo) this year at the Delphi Day event in Italy. In short, you can have a Delphi application with an IdHttpServer component in it to serve maps and map data (like some XML with locations). Now you can see the data in browser or even host TWebBrowser in the application. The goal? You can have a ClientDataSet in the grid, edit data, and see in the map!

You can see a screen shot from the demo program below:

While I cannot post all of the code here, what is relevant is the OnCommandGet event handler of the HTTP server component:

procedure TFormMap.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
fs: tFileStream;
begin
  Caption := ARequestInfo.Document;
  if (ARequestInfo.Document = '/map.htm') then
  begin
    fs := tFileStream.Create ('delphidev.html', fmOpenRead);
    AResponseInfo.ContentStream := fs;
    AResponseInfo.WriteContent;
  end
  else if (ARequestInfo.Document = '/map.xml') then
  begin
    AResponseInfo.ContentText := XmlFromCds;
  end;

The map.htm is a local file with the HTML of the map and the javascript used to populate it, getting data from map.xml on the same server. This request is turned to a custom method that grabs the related data from a ClientDataSet. Now this CDS is populated using Google's own GeoCoding, via HTTP (this time, the client component). For each city in the list box above, it does:

     strResponse := IdHTTP1.Get( TIDUri.UrlEncode(
          'http://maps.google.com/maps/geo?q=' +
          (sListCity.Names[I]) + ', Italy&output=csv&key=******'));
      sList.LineBreak := ',';
      sList.Text := strResponse;
      str1 := sList[2];
      str2 := sList[3];
      cdsTown.AppendRecord([sListCity.Names[I],
        StrToFloat (str1), StrToFloat (str2),
        Length (sListCity.ValueFromIndex[I])]);

It uses Google CVS output format and a StringList with a comma as separator to get longitude and latitude (I'm omitted Google my dev key, of course). If you set the local server port to, say, 8080, you'll be able to navigate to the address:

  WebBrowser1.Navigate('http://127.0.0.1:8088/map.htm');

Now where is the real trick? For any Google maps application to work you need to register the domain. I've registered 127.0.0.1:8088 with my dev key... and it does work! Maybe this is a hack Google will disable, but this has been working for over one year. Now let me know about your mashups, with Delphi clients and servers... or the two combined!





 

23 Comments

Google Maps with a Delphi Server 

Hi Marco,
Great example:-) Any chance you could post the full 
source code to the CodeCentral or somewhere?
Regards,
P
Comment by Pawel Glowacki [http://blogs.codegear.com/pawelglowacki] on June 21, 13:40

Google Maps with a Delphi Server 

can I vote?

I vote YES to Pawel option.  Please post the full
source code to the CodeCentral or somewhere. :-)

My best regards.

jachguate.

Comment by Juan Antonio Castillo Hernández [http://jachguate.wordpress.com] on June 26, 19:38

Google Maps with a Delphi Server 

I'm very  Interesting in you article,  if have your 
Source code , that's better, Please post the full
source code to the CodeCentral or somewhere. :-)

my email:  lam626@163.com
wait your message!
My best regards.

ruofei- from china.


Comment by ruofei on August 6, 10:41

Google Maps with a Delphi Server 

[QUOTE]Now where is the real trick? For any Google
maps application to work you need to register the
domain. I've registered 127.0.0.1:8088 with my dev
key... and it does work! Maybe this is a hack Google
will disable, but this has been working for over one
year. Now let me know about your mashups, with Delphi
clients and servers... or the two combined![/QUOTE]
Actually, I think it's more simple than that : google
doesn't ask for a key if you're using the API in local
mode (not inside a website with a dns)
Any chance you could post the source-code anywhere ? 
Comment by on November 16, 16:30

Google Maps with a Delphi Server 

Hi, great example, how can I obtain the full source code?

Best Regards.

Alexandre Costa
alexandre@brim.com.br
Comment by Alexandre [http://www.brim.com.br] on May 17, 20:24

Google Maps with a Delphi Server 

Thanks for sharing,
I wonder whether this components enable make point on map.
thanks and regards,
mahendra
Comment by mahendra [http://mahendramahfood.blogspot.com] on June 11, 08:50

Google Maps with a Delphi Server 

Hi, in deed a great example, 
how can I obtain the full source code?

Best Regards.

Frank Hufty
frank_hufty@yahoo.com
Comment by HI on June 21, 08:31

Google Maps with a Delphi Server 

Thanks, Marco. This was the only example for using 
Delphi to Geocode with Google that I found in my web 
search, so once again you are the leader in sharing 
great Delphi code.

I was able to get my app to retrieve Geocodes within 
just a few hours with your help. Now I can move 
forward with the rest of my project. With Cantu -- 
Can Do.
Comment by Craig W [http://www.delinium.com] on June 27, 00:48

Google Maps with a Delphi Server 

Interesting article; I read a similar article on the
Google geocoding service on www.scip.be.
I am actually looking for a working Delphi example
that takes a regular street address and returns 
1) the coordinates
2) loads a map into a (embeded) TWebBrowser
Anybody any ideas?
Regards,
Koen
Comment by Koen Verbeeck on July 8, 23:50

Google Maps with a Delphi Server 

 Thanks a lot

i take a few minutes to make this example, is so 
usefull.
Comment by Gemilio [http://www.control-maestro.com] on July 10, 18:01

Google Maps with a Delphi Server 

Hi Marko, I'm very interesting in you article. Can I 
obtain the full source code?

Best Regards.
Seud
sfazlic@linear-software.de
Comment by Seud on September 17, 18:24

Google Maps with a Delphi Server 

 Hi Marko, Can I get the full sourse code?

I´m working in a projetc that takes distance between 
several sequence address and returns it.

 Regards,
 Adrian
adriano_m_349@hotmail.com
Comment by Adrian on May 7, 01:23

Google Maps with a Delphi Server 

Hi Marco,

l have been trying to built something similar to you 
application to no avail... Could youy kindly send me 
a copy of the full source on mwemmicip@gmail.com? l 
will appreciate alot.

Regards,
Wemmic
Comment by Wemmic on June 7, 20:18

Google Maps with a Delphi Server 

 Hello, Marko! 
Could You send me your example too?
I can share my similar projects!

vgsh@inbox.ru
Best Regards!
Comment by on July 17, 15:58

Google Maps with a Delphi Server 

 Hi Marco,

 Could youy kindly send me a copy of the full source 
too.
my email: weitaofan@gmail.com
thanks
Regards,
FAN
Comment by on September 23, 21:09

Google Maps with a Delphi Server 

Hello, Marko!
I'm very interested in this article.
Could You send me your example too?

r_ob82@wp.pl
Best Regards!
Comment by Robert on September 26, 17:16

Google Maps with a Delphi Server 

Bele za de exemplo Marco, onde consigo o exemplo
completo com os fontes ?

att,

Sérgio
sernet@gmail.com
Comment by Sérgio Lopes on October 8, 05:56

Google Maps with a Delphi Server 

Hi, Nice work, please send me full source on e mail 
the-elzabackdoor@hotmail.com
Comment by Glamor on November 8, 20:18

Google Maps with a Delphi Server 

Hi, very nice work, could you send me full source ?
colibasili@msn.com
Comment by murat on December 1, 00:57

Google Maps with a Delphi Server 

 Hi Marco, I would like you to send me the full 
source code to edgwin@gmail.com please!

Thanks in advance!
Comment by Edgwin on December 10, 01:24

Google Maps with a Delphi Server 

 Hi Marco, very nice work.
could you send me the full source code.
Thanks.
My email: gebhard@blankedv.at
Comment by Gebhard Blank [http://www.blankedv.at] on January 17, 18:07

Google Maps with a Delphi Server 

Hi Marco,
wery nice work.
Would be nice to share your source code through the web 
site
Comment by Adem HUR [http://www.amatis.com.tr/] on October 22, 09:01

Google Maps with a Delphi Server 

Code added to my public repository... description is in 
much newer blog post at 
http://blog.marcocantu.com/blog/delphi_mapping_rest_clie
nts.html (see link in name above)
Comment by Marco Cantu [http://blog.marcocantu.com/blog/delphi_mapping_rest_clients.html] on February 3, 22:38


Post Your Comment

Click here for posting your feedback to this blog.

There are currently 0 pending (unapproved) messages.