Here is a simple program to show how you can “host” google maps in a TWebBrowser and interact with it from a Delphi application.
Download the Delphi project
I am a big fan of self contained EXEs, saving to a file and then loading that file in a TWebBrowser did not seem necessary. My first thought was to put an Indy TIdHTTPServer and serve the page from the code. This worked, but it activated the windows Firewall and asked me if I wanted to unblock, which annoyed me.
My next thought was to load a string directly into the TWebBrowser, as I had done before, following: How to load HTML directly to a WebBrowser, but then I started getting the Google Maps error that “The Google Maps API key used on this web site was registered for a different web site. …” I found a trick to get around that by making the TWebBrowser think its URL was http://localhost, by setting the IHTMLDocument2.URL after the .Navigate() to about:blank
WebBrowser.Navigate('about:blank'); (WebBrowser.Document as IHTMLDocument2).URL := 'http://localhost/';
That was all it needed, now the Delphi app was completely self contained.
Geocoding using Google from Delphi
I knew that Google allowed geocoding using your API key, so I signed up for another API key (I can’t find anywhere to retrieve the keys I’ve generated) and quickly made an example which retrieved the comma-separated values from http://maps.google.com/maps/geo?q=[Encoded address here]&output=csv&key=… and loaded the two edit boxes. The “Add Marker” button simply puts a marker on the map at the latitude and longitude specified in the edit boxes, and the “Center map on” button simply calls map.panTo. I may update this to have a more usable marker being added, since right now it doesn’t have a callback for clicking or anything, it’s just a visual representation.
I knew it had to be possible, and I wanted to give out a fully working example, without giving out an API key tied to my gmail address, so in some quick google searches, I found some php code which scraped the resulting page that google returns when a user types into the search box at the main page of googlemaps. So I copied and pasted my other geocoding function and used http://maps.google.com/maps?q= as the link, and searched through the html response for the center point, which would be the latitude and longitude of the address entered. I parsed out the values, and I was a little surprised that when searching on the same address through these two different ways, the geocoding API was less accurate, typing a town name into google maps search box put the Marker just about directly over the town name that is drawn on the map, but through the geocoding service, it was off a little. So this way may be a little more accurate, but it will break if Google changes the structure of the page.
I noticed something else strange, when I was looking through the code, I noticed that my “cheat” geocoding URL was adding &output=csv because I copied and pasted it. I didn’t think it was necessary, as I could leave it off and Firefox would bring me to the page of the map without problems. When I removed it from the code, however, Indy kept throwing “HTTP/1.1 302 Found.” Exceptions. I thought maybe it just needed another dummy parameter after the q=, but everything else I tried caused the Exceptions.
I do suggest you stick to the geocode API, since it has a much less likelihood of breaking if Google decides to change something, and it’s the accepted way to use their free service.
I hope this helps satisfy the people who wanted to see the source for a working example of using Google Maps in a Delphi Application.