KhafizovTT

координаты google maps

Oct 25th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function GetCoordinates(Address As String) As String
  2.    
  3.     '-----------------------------------------------------------------------------------------------------
  4.    'This function returns the latitude and longitude of a given address using the Google Geocoding API.
  5.    'The function uses the "simplest" form of Google Geocoding API (sending only the address parameter),
  6.    'so, optional parameters such as bounds, key, language, region and components are NOT used.
  7.    'In case of multiple results (for example two cities sharing the same name), the function
  8.    'returns the FIRST OCCURRENCE, so be careful in the input address (tip: use the city name and the
  9.    'postal code if they are available).
  10.    
  11.     'NOTE: As Google points out, the use of the Google Geocoding API is subject to a limit of 2500
  12.    'requests per day, so be careful not to exceed this limit.
  13.    'For more info check: https://developers.google.com/maps/documentation/geocoding
  14.    
  15.     'In order to use this function you must enable the XML, v3.0 library from VBA editor:
  16.    'Go to Tools -> References -> check the Microsoft XML, v3.0.
  17.    
  18.     'Written by:    Christos Samaras
  19.    'Date:          12/06/2014
  20.    'e-mail:        xristos.samaras@gmail.com
  21.    'site:          http://www.myengineeringworld.net
  22.    '-----------------------------------------------------------------------------------------------------
  23.    
  24.     'Declaring the necessary variables. Using 30 at the first two variables because it
  25.    'corresponds to the "Microsoft XML, v3.0" library in VBA (msxml3.dll).
  26.    Dim Request         As New XMLHTTP30
  27.     Dim Results         As New DOMDocument30
  28.     Dim StatusNode      As IXMLDOMNode
  29.     Dim LatitudeNode    As IXMLDOMNode
  30.     Dim LongitudeNode   As IXMLDOMNode
  31.            
  32.     On Error GoTo errorHandler
  33.    
  34.     'Create the request based on Google Geocoding API. Parameters (from Google page):
  35.    '- Address: The address that you want to geocode.
  36.    '- Sensor: Indicates whether your application used a sensor to determine the user's location.
  37.    'This parameter is no longer required.
  38.    Request.Open "GET", "http://maps.googleapis.com/maps/api/geocode/xml?" _
  39.     & "&address=" & RussianStringToURLEncode(Address) & "&sensor=false&region=ru", False
  40.    
  41.     'Send the request to the Google server.
  42.    Request.send
  43.    
  44.     'Read the results from the request.
  45.    Results.LoadXML Request.responseText
  46.    
  47.     'Get the status node value.
  48.    Set StatusNode = Results.SelectSingleNode("//status")
  49.    
  50.     'Based on the status node result, proceed accordingly.
  51.    Select Case UCase(StatusNode.Text)
  52.    
  53.         Case "OK"   'The API request was successful. At least one geocode was returned.
  54.            
  55.             'Get the latitdue and longitude node values of the first geocode.
  56.            Set LatitudeNode = Results.SelectSingleNode("//result/geometry/location/lat")
  57.             Set LongitudeNode = Results.SelectSingleNode("//result/geometry/location/lng")
  58.            
  59.             'Return the coordinates as string (latitude, longitude).
  60.            GetCoordinates = LatitudeNode.Text & ", " & LongitudeNode.Text
  61.        
  62.         Case "ZERO_RESULTS"   'The geocode was successful but returned no results.
  63.            GetCoordinates = "Возможно адрес не существует"
  64.            
  65.         Case "OVER_QUERY_LIMIT" 'The requestor has exceeded the limit of 2500 request/day.
  66.            GetCoordinates = "Исчерпан лимит суточных запросов"
  67.            
  68.         Case "REQUEST_DENIED"   'The API did not complete the request.
  69.            GetCoordinates = "Сервер отклонил запрос"
  70.            
  71.         Case "INVALID_REQUEST"  'The API request is empty or is malformed.
  72.            GetCoordinates = "Запрос не корректен или пуст"
  73.        
  74.         Case "UNKNOWN_ERROR"    'Indicates that the request could not be processed due to a server error.
  75.            GetCoordinates = "Неизвестная ошибка"
  76.        
  77.         Case Else   'Just in case...
  78.            GetCoordinates = "Ошибка"
  79.        
  80.     End Select
  81.        
  82.     'In case of error, release the objects.
  83. errorHandler:
  84.     Set StatusNode = Nothing
  85.     Set LatitudeNode = Nothing
  86.     Set LongitudeNode = Nothing
  87.     Set Results = Nothing
  88.     Set Request = Nothing
  89.    
  90. End Function
Add Comment
Please, Sign In to add comment