1

I have a Delphi application which loads a Google map in a TWebBrowser:

with WebBrowser1.Document as IHTMLDocument2 do
  with parentWindow do
    ExecScript('map.setCenter(new GLatLng(51.15917, 4.13889),10)', 'JavaScript');

Any idea of how to get the extents of the map in my application?
TIA
Steven

edit
Rob's answer points to a partial solution: javascript knows about the coordinates. I would like to get them into my Delphi application.

note on Expert's Exchange
(long answer to davidins' reply)
I was on Expert's Exchange when they started and when it was still free. I left there when they got greedy. If somebody is nice enough to help me out on a technical problem, I don't mind paying the guy a drink for it, but I definitely don't want to pay somebody else for it.
And their 30-day trial is even worse. Why do I have to submit my credit card number if they're not going to charge it?
"Experts Exchange is the most trusted IT Resource on the internet and we are confident that you will agree" (sic). Well, I wouldn't be too sure, EE. I like SO a lot more, appreciate any help I can get and try to give answers whenever I can (which unfortunately is not often).

stevenvh
  • 2,971
  • 9
  • 41
  • 54
  • 1
    Please see http://stackoverflow.com/questions/1083472/finding-the-lat-long-of-the-corners-in-a-google-maps-window – Rob Kennedy Jul 06 '09 at 16:38
  • 2
    Find the E-E question via Google, and the answer will be visible. E-E displays the answer if the referrer is google.com. – Rob Kennedy Jul 06 '09 at 19:14
  • This is correct. However I updated my answer to contain the solution for you anyway :) (hopefully it's the solution you're looking for at least) – davidivins Jul 06 '09 at 21:08
  • It used to be I'd just skip over all those expert sex change results in google because I though you had to pay to see the answers. But it was only recently that I learned that all you have to do is press page down about a million times because the answer is printed free at the bottom of the page after about 10,000 lines of crap. Just a little tip for those who were like me for many months... – priestc Jul 06 '09 at 21:24

2 Answers2

3

I was going to suggest that you use the fact that javascript knows the coordinates to somehow execute a script with that call in order to return the coordinates to your app. however, after looking at this thread - expert's exchange, it seems like that's not possible. but that same thread says "you can have your JavaScript function set the value of an HTML hidden field element, and then read that value from your Delphi code." the example is down towards the bottom. hope that's a little helpful. definitely not an ideal way to get the coordinates though.

edit: sorry, i didn't realize when you go to expert's exchange from here, it blocks the content. if you visit that page from google, it will show up. just to save you the time, here's a copy and paste of the solution:

DropZone: I don't think there's a way to get the value of a JavaScript function directly, since the functions are executed through various magical layers of abstraction. However you can have your JavaScript function set the value of an HTML hidden field element, and then read that value from your Delphi code.

The first part is trivially simple: you just have the JavaScript function set the value on a hidden field:



<!-- Somewhere in the HTML form -->
<input type="hidden" id="HiddenFieldID" name="HiddenFieldName" value="">

--------------------

// JavaScript function
function MyFunc()
{
     // Get the hidden field by its ID.
     var elm = document.getElementById('HiddenFieldID');
     if (elm) elm.value = '10086';

     return 10086;
}

Here's an example on how to do this last part: http://www.cryer.co.uk/brian/delphi/twebbrowser/read_write_form_elements.htm#GetValueOfField

In your case, you'll want to have something like the attached code.

-dZ.


function GetFieldValue(fromForm: IHTMLFormElement;
    const fieldName: string): string;
var
  field: IHTMLElement;
  inputField: IHTMLInputElement;
  selectField: IHTMLSelectElement;
  textField: IHTMLTextAreaElement;
begin
  field  := fromForm.Item(fieldName,'') as IHTMLElement;
  result := '';
  if Assigned(field) and (field.tagName = 'INPUT') then
  begin
    inputField := field as IHTMLInputElement;
    if inputField.type_ = 'hidden' then
      result := inputField.value;
  end
end;
davidivins
  • 321
  • 1
  • 7
  • 1
    Sorry, no access to Expert's Exchange. Can't you post the solution here? (see long answer appended to OP) – stevenvh Jul 06 '09 at 18:34
  • 1
    David has already included the solution: Set the value of a hidden HTML form field, and then read that element's value from the host application. – Rob Kennedy Jul 06 '09 at 19:41
0

IHTMLWindow2.execScript from the mentioned EE example should return the return value of the executed script as a Variant. But you don't have to use IHTMLDocument2.parentWindow property. There's also IHTMLDocument.Script which is an IDispatch so you can use it via Variant late binding:

var
  Document: IHTMLDocument;
  VScript, V: Variant;
begin
  Document := WebBrowser.Document as IHTMLDocument;
  VScript := Document.Script;
  V := VScript.HelloJavaScript();
  ShowMessage(V);
end;

HelloJavaScript is a javascript function returning a string:

<script language="javascript">
function HelloJavaScript()
{
    s = "Hello, world! (javascript)";
        alert(s);
        return s;
}
</script>
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • Maybe ExecScript *should* return the result as a Variant, but it doesn't. The function is documented to always set the result parameter to VT_EMPTY. – Rob Kennedy Jul 07 '09 at 15:00
  • That's interesting, thanks. Fortunately, the IHTMLDocument.Script method works. – Ondrej Kelle Jul 07 '09 at 15:07