Google Maps is a huge map database / GIS. They have digitized map data and satelite images of the whole world in an amazing quality. I got several support questions how to access the Google API from IntraWeb, so I thougt it was a good idea to provide a demo that shows how to display Google maps in IntraWeb applications. Source is attached.

The techniques/tricks in this demo can be used in any similar environment where you need to reference external libraries. This demo works for IntraWeb 8.0 and 9.0. If you have 9.0, then you can see the advantage of using IntraWeb’s Ajax support.

See a live demo here.

To use Google’s Map API, you have to register for a free key. This key is per domain, so if you want to test locally with this demo application, then you have to apply for a “127.0.0.1:8888″ key:

Google Maps API signup

In your IntraWeb application you have to add a reference to the API JavaScript library, which is the tricky part. This reference must be available before you can call any API method. This is done in IWForm.OnRender using “JavaScriptOnce”.

The actual Map object is the created in “PageContext.AddToInitProc” calls. The code of these calls is inserted before all other code inserted via AddToInitProc.

procedure TFormMain.IWAppFormRender(Sender: TObject);
begin
  //This is a trick to inject a script file refererence here
  JavaScriptOnce :=
    ‘</script>’ +
    ‘<script src=”http://maps.google.com/maps?file=api&v=2&key=’+GoogleKey+’” type=”text/javascript”></script>’ +
    ‘<script type=”text/javascript”>’;

  //IMPORTANT: use AddToInitProc of *PageContext*
  //   This is rendered after the Google Script reference, but before
  //   “regular” AddToInitProc calls, as used in the OnClick events
  PageContext.AddToInitProc(’map = new GMap2(document.getElementById(”MYGOOGLEMAP”));’);
  //The geocoder / showAddress is used to locate a textual address.
  PageContext.AddToInitProc(’geocoder = new GClientGeocoder();’);
end;

Get the full source here: GoogleMapAPI.zip

Viewing 1 Comment

Trackbacks

close Reblog this comment
blog comments powered by Disqus
CodeGear Technology Partner