Delphi 2007 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!





 

4 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


Post Your Comment

Click here for posting your feedback to this blog.

There are currently 0 pending (unapproved) messages.