在Excel VBA中进行地理编码以返回location_type?

目前使用此人的地理编码器。

http://www.myengineeringworld.net/2014/06/geocoding-using-vba-google-api.html

它工作得很好,但是我也想包括'location_type',这是准确性(ROOFTOP,COUNTRY)。 当然,这是非常专业的,虽然我有一些VBA和XML技能,但我没有任何使用任何API的经验。

我希望输出是'lat,long,accuracy'。

以下是上述网站的代码(不是矿山)。

Option Explicit Function GetCoordinates(Address As String) As String '----------------------------------------------------------------------------------------------------- 'This function returns the latitude and longitude of a given address using the Google Geocoding API. 'The function uses the "simplest" form of Google Geocoding API (sending only the address parameter), 'so, optional parameters such as bounds, key, language, region and components are NOT used. 'In case of multiple results (for example two cities sharing the same name), the function 'returns the FIRST OCCURRENCE, so be careful in the input address (tip: use the city name and the 'postal code if they are available). 'NOTE: As Google points out, the use of the Google Geocoding API is subject to a limit of 2500 'requests per day, so be careful not to exceed this limit. 'For more info check: https://developers.google.com/maps/documentation/geocoding 'In order to use this function you must enable the XML, v3.0 library from VBA editor: 'Go to Tools -> References -> check the Microsoft XML, v3.0. 'Written by: Christos Samaras 'Date: 12/06/2014 'e-mail: xristos.samaras@gmail.com 'site: http://www.myengineeringworld.net '----------------------------------------------------------------------------------------------------- 'Declaring the necessary variables. Using 30 at the first two variables because it 'corresponds to the "Microsoft XML, v3.0" library in VBA (msxml3.dll). Dim Request As New XMLHTTP30 Dim Results As New DOMDocument30 Dim StatusNode As IXMLDOMNode Dim LatitudeNode As IXMLDOMNode Dim LongitudeNode As IXMLDOMNode On Error GoTo errorHandler 'Create the request based on Google Geocoding API. Parameters (from Google page): '- Address: The address that you want to geocode. '- Sensor: Indicates whether your application used a sensor to determine the user's location. 'This parameter is no longer required. Request.Open "GET", "http://maps.googleapis.com/maps/api/geocode/xml?" _ & "&address=" & Address & "&sensor=false", False 'Send the request to the Google server. Request.send 'Read the results from the request. Results.LoadXML Request.responseText 'Get the status node value. Set StatusNode = Results.SelectSingleNode("//status") 'Based on the status node result, proceed accordingly. Select Case UCase(StatusNode.Text) Case "OK" 'The API request was successful. At least one geocode was returned. 'Get the latitdue and longitude node values of the first geocode. Set LatitudeNode = Results.SelectSingleNode("//result/geometry/location/lat") Set LongitudeNode = Results.SelectSingleNode("//result/geometry/location/lng") 'Return the coordinates as string (latitude, longitude). GetCoordinates = LatitudeNode.Text & ", " & LongitudeNode.Text Case "ZERO_RESULTS" 'The geocode was successful but returned no results. GetCoordinates = "The address probably not exists" Case "OVER_QUERY_LIMIT" 'The requestor has exceeded the limit of 2500 request/day. GetCoordinates = "Requestor has exceeded the server limit" Case "REQUEST_DENIED" 'The API did not complete the request. GetCoordinates = "Server denied the request" Case "INVALID_REQUEST" 'The API request is empty or is malformed. GetCoordinates = "Request was empty or malformed" Case "UNKNOWN_ERROR" 'Indicates that the request could not be processed due to a server error. GetCoordinates = "Unknown error" Case Else 'Just in case... GetCoordinates = "Error" End Select 'In case of error, release the objects. errorHandler: Set StatusNode = Nothing Set LatitudeNode = Nothing Set LongitudeNode = Nothing Set Results = Nothing Set Request = Nothing End Function '-------------------------------------------------------------------------- 'The next two functions using the GetCoordinates function in order to get 'the latitude and the longitude correspondingly of a given address. '-------------------------------------------------------------------------- Function GetLatidue(Address As String) As Double Dim Coordinates As String 'Get the coordinates for the given address. Coordinates = GetCoordinates(Address) 'Return the latitude as number (double). If Coordinates <> "" Then GetLatidue = CDbl(Left(Coordinates, WorksheetFunction.Find(",", Coordinates) - 1)) End If End Function Function GetLongitude(Address As String) As Double Dim Coordinates As String 'Get the coordinates for the given address. Coordinates = GetCoordinates(Address) 'Return the longitude as number (double). If Coordinates <> "" Then GetLongitude = CDbl(Right(Coordinates, Len(Coordinates) - WorksheetFunction.Find(",", Coordinates))) End If End Function 

当然,这里有很多借用的代码是人们想要扩展的。 信贷的作者或来源(如你),张贴在这里,让人们不必stream浪

尝试这个:

 Case "OK" 'The API request was successful. At least one geocode was returned. 'Get the latitdue and longitude node values of the first geocode. Set LatitudeNode = Results.SelectSingleNode("//result/geometry/location/lat") Set LongitudeNode = Results.SelectSingleNode("//result/geometry/location/lng") 'Get the Location Type ' Not sure if this variable needs to be defined as a global somewhere else? ' If so, it would be right by definition of LatitudeNode and LongitudeNode Set LocationType = = Results.SelectSingleNode("//result/geometry/location_type") 'Return the coordinates as string (latitude, longitude). ' and add location type GetCoordinates = LatitudeNode.Text & ", " & LongitudeNode.Text & ", " & LocationType.Text