读取距离xml节点(距离)-谷歌地图api-Delphi 6

读取距离xml节点(距离)-谷歌地图api-Delphi 6,xml,delphi,google-maps-api-3,Xml,Delphi,Google Maps Api 3,我在Excel中使用以下代码返回两点(起点和终点)之间的距离 在Delphi 6中有类似的方法来实现这一点吗 我在网上找到了几个例子,但都使用了Delphi6中不存在的函数,可能是因为它太旧了。例如,Delphi 6无法识别的IHTMLDocument2 Function Km_Distance(Origin As String, Destination As String) As Double Dim myRequest As XMLHTTP Dim myDo

我在Excel中使用以下代码返回两点(起点和终点)之间的距离

在Delphi 6中有类似的方法来实现这一点吗

我在网上找到了几个例子,但都使用了Delphi6中不存在的函数,可能是因为它太旧了。例如,Delphi 6无法识别的IHTMLDocument2

  Function Km_Distance(Origin As String, Destination As String) As Double       
    Dim myRequest As XMLHTTP
    Dim myDomDoc As DOMDocument
    Dim distanceNode As IXMLDOMNode

    Let Km_Distance = 0

    On Error GoTo exitRoute

    Let Origin = Replace(Origin, " ", "%20")
    Let Destination = Replace(Destination, " ", "%20")        

    Set myRequest = New XMLHTTP

    myRequest.Open "GET", "http://maps.googleapis.com/maps/api/directions/xml?origin=" _
        & Origin & "&destination=" & Destination & "&sensor=false", False
    myRequest.send

    Set myDomDoc = New DOMDocument

    myDomDoc.LoadXML myRequest.responseText

    Set distanceNode = myDomDoc.SelectSingleNode("//leg/distance/value")
    If Not distanceNode Is Nothing Then Km_Distance = distanceNode.Text / 1000

exitRoute:
    Set distanceNode = Nothing
    Set myDomDoc = Nothing
    Set myRequest = Nothing
End Function
我可以读取API返回的xml(下面的代码)

现在的问题是:如何在Delphi上读取xml中的距离? 相当于: 设置distanceNode=myDomDoc.SelectSingleNode(“//腿/距离/值”)


以下是我的作品:

Uses ... MSXML, UrlMon, ...

function TForm1.GetURL : String;
var
  Origin,
  Destination,
  URL : String;
begin
  Origin := StringReplace(edOrigin.Text, ' ', '#20', [rfReplaceAll]);
  Destination := StringReplace(edDestination.Text, ' ', '#20', [rfReplaceAll]);
  Result :=  GoogleMapsURL + '&origin=' + Origin + '&destination=' + Destination
    + '&sensor=false';
end;

procedure TForm1.GetXML;
var
  XMLDoc: IXMLDOMDocument;
  Node : IXMLDomNode;
  XMLUrl : String;
  XMLFileName: String;
begin

 XMLUrl := GetUrl;

 XMLFileName := 'C:\temp\temp.xml';

 URLDownloadToFile(Nil, PChar(XMLUrl), PChar(XMLFileName), 0, Nil);

 Memo1.Lines.LoadFromFile(XMLFileName);

 XMLDoc := CoDOMDocument.Create;
 XMLDoc.LoadXML(Memo1.Lines.Text);
 Node := XMLDoc.SelectSingleNode('//leg/distance/value');

 Memo1.Lines.Add(Node.Text);
end;
我使用
URLDownloadToFile
下载XML,因为出于某种原因
XMLDoc.Load(GetURL)
返回一个空的XML文档。我还尝试将URL加载到TWebBrowser中,但尝试从中检索IHtmlDocument2接口也失败了。我不知道这些问题是否特定于googlemaps URL,通常它们(XMLDoc.Load和TWebBrowser的IHtlmDocument2接口)都可以正常工作

更新:我使用
XMLDoc.Load
从谷歌地图url获取XML时遇到的困难似乎是因为我使用的导入单元MSXML已经过时。以下内容在Windows10 64位上运行,避免了使用
URLDownloadToFile
(尽管实际上我更喜欢使用它,因为这样我就有了XML)

程序TForm1.GetXML2;
变量
XMLDoc:IXMLDOMDocument;
节点:IXMLDomNode;
XMLUrl:字符串;
XMLFileName:String;
开始
XMLDoc:=CreateOleObject('Msxml2.DOMDocument.6.0')作为IXMLDOMDocument;
XMLDoc.async:=False;
XMLUrl:=GetUrl;
如果不是XMLDoc.load(XMLUrl),则
出口
断言(XMLDoc.DocumentElement nil);
Node:=XMLDoc.SelectSingleNode('//leg/distance/value');
备注1.Lines.Add(Node.Text);
结束;

“IHTMLDocument2..Delphi 6无法识别”如果项目设置正确,它将识别。IHTMLDocument2是Delphi附带的单元MSHTML.Pas中定义的许多接口之一(我没有D6,但它肯定是D5附带的)。您需要将MSHTML添加到Delphi代码的使用列表中(可能还需要将其路径添加到您的项目设置中)。在my Delphi 6上,显示:未声明的标识符:“IXMLDOMDocument”未声明的标识符:“IXMLDomNode”是必需的添加更多关于“使用”的引用?非常感谢。它工作得很好。我只是需要对我的情况做一些调整,但这正是我所需要的。
Uses ... MSXML, UrlMon, ...

function TForm1.GetURL : String;
var
  Origin,
  Destination,
  URL : String;
begin
  Origin := StringReplace(edOrigin.Text, ' ', '#20', [rfReplaceAll]);
  Destination := StringReplace(edDestination.Text, ' ', '#20', [rfReplaceAll]);
  Result :=  GoogleMapsURL + '&origin=' + Origin + '&destination=' + Destination
    + '&sensor=false';
end;

procedure TForm1.GetXML;
var
  XMLDoc: IXMLDOMDocument;
  Node : IXMLDomNode;
  XMLUrl : String;
  XMLFileName: String;
begin

 XMLUrl := GetUrl;

 XMLFileName := 'C:\temp\temp.xml';

 URLDownloadToFile(Nil, PChar(XMLUrl), PChar(XMLFileName), 0, Nil);

 Memo1.Lines.LoadFromFile(XMLFileName);

 XMLDoc := CoDOMDocument.Create;
 XMLDoc.LoadXML(Memo1.Lines.Text);
 Node := XMLDoc.SelectSingleNode('//leg/distance/value');

 Memo1.Lines.Add(Node.Text);
end;
procedure TForm1.GetXML2;
var
  XMLDoc: IXMLDOMDocument;
  Node : IXMLDomNode;
  XMLUrl : String;
  XMLFileName: String;
begin
 XMLDoc := CreateOleObject('Msxml2.DOMDocument.6.0') as IXMLDOMDocument;
 XMLDoc.async := False;

 XMLUrl := GetUrl;
 if not XMLDoc.load(XMLUrl) then
   exit;

 Assert(XMLDoc.DocumentElement <> nil);

 Node := XMLDoc.SelectSingleNode('//leg/distance/value');

 Memo1.Lines.Add(Node.Text);

end;