maps 您所在的位置:网站首页 api计算 maps

maps

2023-07-21 05:38| 来源: 网络整理| 查看: 265

maps-api-v3

I have a potential client which reimburses employ travel based on the distance between their office and homes they visit as part of their job.  They have found that many of the distances included on expense accounts do not reflect the shortest distance between start and end points, and they want a way to use the distances calculated by Google Maps to standardize these mileage values.

我有一个潜在的客户,根据其办公室和他们所访问的房屋之间的距离,报销雇用其工作的差旅费用。 他们发现,费用帐户中包含的许多距离并不能反映起点和终点之间的最短距离,因此,他们想要一种方法来使用Google Maps计算的距离来标准化这些里程值。

There are many websites which provide direction, distance, and time estimates to get from point A to point B while driving.  Google Maps is just one these services, many of which provide APIs that allow developers and organizations to tap into these features in their own applications.  

许多网站都提供行车路线,距离和时间估算,以便在驾车时从A点到达B点。 Google Maps只是这些服务之一,其中许多提供API,允许开发人员和组织在自己的应用程序中利用这些功能。

Google recently modified its policy for using Google Maps and is now charging on a "pay-as-you-go" basis.  On their web site, they indicate that they provide $200 in free usage credits per month, and that "for most of our users, the $200 monthly credit is enough to support their needs."  With this knowledge in hand, I gave them my credit card info, created a new account, and got a license key (which is used in the XML request).

Google最近修改了使用Google Maps的政策,现在按“现收现付”收费。 他们在其网站上表示,他们每月提供200美元的免费使用额度,“对于我们大多数用户来说,每月200美元的额度足以满足他们的需求”。 掌握了这些知识后,我给了他们我的信用卡信息,创建了一个新帐户,并获得了一个许可证密钥(用于XML请求)。

But when I started looking for code example, I found very few VBA examples.  I did find one example of how to use the directions feature so with that code in hand, I created a simple form in a new project file to test this new feature.

但是,当我开始寻找代码示例时,发现了很少的VBA示例。 我确实找到了一个如何使用路线功能的示例,因此,有了该代码,我在新项目文件中创建了一个简单表单来测试此新功能。

I then added a reference in the project to the MS XML, v6.0 library and headed off to the races.

然后,我在项目中添加了对MS XML v6.0库的引用,然后开始比赛。

The code behind the 'Distance' button simply calls the function (shown below) which, although it does not include any error handlers,  provides all of the details you need to get your application running.

“距离”按钮后面的代码仅调用该函数(如下所示),该函数虽然不包括任何错误处理程序,但提供了使应用程序运行所需的所有详细信息。

Public Function GoogleDistance(FromAddress As String, ToAddress As String) As String     Dim strKey As String     Dim sXMLURL As String     Dim objXMLHTTP As MSXML2.ServerXMLHTTP         On Error GoTo ProcError         strKey = "your license key goes here"     sXMLURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?" _             & "units=imperial&" _             & "origins=" & Replace(FromAddress, " ", "") & "&" _             & "destinations=" & Replace(ToAddress, " ", "") & "&" _             & "key=" & strKey 'Debug.Print sXMLURL     Set objXMLHTTP = New MSXML2.ServerXMLHTTP         With objXMLHTTP         .Open "Get", sXMLURL, False         .setRequestHeader "content-Type", "application/x-www-form-URLEncoded"         .send     End With     'Debug.Print objXMLHTTP.responseText     Dim domResponse As DOMDocument60     Set domResponse = New DOMDocument60     domResponse.loadXML objXMLHTTP.responseText         Dim ixnStatus As Variant     Set ixnStatus = domResponse.selectSingleNode("//status")         If ixnStatus.Text = "OK" Then         Dim ixnDistance, ixnDuration         Set ixnDistance = domResponse.selectSingleNode("/DistanceMatrixResponse/row/element/distance/text")         Set ixnDuration = domResponse.selectSingleNode("/DistanceMatrixResponse/row/element/duration/text")     End If         Forms("form1").txt_Distance = ixnDistance.Text     Forms("form1").txt_duration = ixnDuration.Text     ProcExit:     Exit Function     ProcError:     Debug.Print Err.Number, Err.Description     MsgBox Err.Number & vbCrLf & Err.Description     Resume ProcExit     Resume End Function

I've included a couple of debug.print statements in the code to allow you to see the actual values which are passed to the request and received back as the response.  If I were going to implement this within an application, I would obviously add some error handling code which would provide meaningful error messages back to the user.

我在代码中包含了几个debug.print语句,使您可以查看传递给请求并作为响应接收回的实际值。 如果要在应用程序中实现此功能,很明显我会添加一些错误处理代码,这些代码将向用户提供有意义的错误消息。

Take a look at the attached .accde file and see how simple it is to include this feature in your application.

查看附带的.accde文件,并查看在应用程序中包含此功能有多么简单。

GoogleMapDistance.accde

GoogleMapDistance.accde

翻译自: https://www.experts-exchange.com/articles/33152/Computing-road-distances-using-the-Google-Maps-DistanceMatrix-API.html

maps-api-v3



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有