如何获得地址的纬度和经度?

时间:2014-03-03 09:11:04

标签: google-sheets geocoding

我目前正在处理数据列表,我的一个数据列是列表的地址。我计划在Google地图上绘制每个地址,但我希望避免手动将地址粘贴到Google以获取参数。我需要经度,纬度。我的Excel工作表(或我在Google Docs电子表格中有它)包含大约500个地址。

所以我希望有一些事情可以自动完成吗?

4 个答案:

答案 0 :(得分:10)

礼貌的这篇伟大的帖子 http://policeanalyst.com/using-the-google-geocoding-api-in-excel/

Function GoogleGeocode(address As String) As String
Dim strAddress As String
Dim strQuery As String
Dim strLatitude As String
Dim strLongitude As String

strAddress = URLEncode(address)

'Assemble the query string
strQuery = "http://maps.googleapis.com/maps/api/geocode/xml?"
strQuery = strQuery & "address=" & strAddress 
strQuery = strQuery & "&sensor=false"

'define XML and HTTP components
Dim googleResult As New MSXML2.DOMDocument
Dim googleService As New MSXML2.XMLHTTP
Dim oNodes As MSXML2.IXMLDOMNodeList
Dim oNode As MSXML2.IXMLDOMNode

'create HTTP request to query URL - make sure to have
'that last "False" there for synchronous operation

googleService.Open "GET", strQuery, False
googleService.send
googleResult.LoadXML (googleService.responseText)

Set oNodes = googleResult.getElementsByTagName("geometry")

If oNodes.Length = 1 Then
For Each oNode In oNodes
  strLatitude = oNode.ChildNodes(0).ChildNodes(0).Text
  strLongitude = oNode.ChildNodes(0).ChildNodes(1).Text
  GoogleGeocode = strLatitude & "," & strLongitude
 Next oNode
 Else
 GoogleGeocode = "Not Found (try again, you may have done too many too fast)"
 End If
 End Function


Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String
Dim StringLen As Long: StringLen = Len(StringVal)

If StringLen>0 Then
ReDim result(StringLen) As String
Dim i As Long, CharCode As Integer
Dim Char As String, Space As String

If SpaceAsPlus Then Space = "+" Else Space = "%20"

For i = 1 To StringLen
  Char = Mid$(StringVal, i, 1)  
  CharCode = Asc(Char)

  Select Case CharCode
  Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
    result(i) = Char
  Case 32
    result(i) = Space
  Case 0 To 15
    result(i) = "%0" & Hex(CharCode)
  Case Else
    result(i) = "%" & Hex(CharCode)
  End Select
Next i
URLEncode = Join(result, "")
End If
End Function

答案 1 :(得分:6)

您可以在google电子表格中添加一些googl apps脚本,以便它能够为您完成工作。 here一个演示(添加一个地址,它会为你进行地理编码) 在代码下方执行地理编码:

function getLatLong(adress) {
  try{
    if(adress=="")return("");
    var geo = Maps.newGeocoder().geocode(adress);
    if(geo.status=="OK"){
      var lng = geo.results[0].geometry.viewport.southwest.lng;
      var lat = geo.results[0].geometry.viewport.southwest.lat;
      return([lat,lng]);
    }
    else{
      return("error");
    }
  }
  catch(err){
    return(err);
  }
}

答案 2 :(得分:1)

SmartyStreets提供美国和国际地址的地理编码。它非常易于使用 - 将您的列表粘贴到他们的网络工具中,并在几秒钟内返回纬度和经度。还有其他服务可以以类似的方式返回纬度/经度数据;我最熟悉SmartyStreets,因为我在那里工作。

另请注意,将地址粘贴到Google地图中并不一定能为您提供准确的信息,因为Google首先验证地址; SmartyStreets确实可以确保您获得有关真实位置的数据,而不是Google的最佳猜测。

您可能希望查看的其他解决方案:AddressDoctorLoqate或其他“国际地址地理编码”提供商(快速Google搜索会提供一些很好的选择)。

答案 3 :(得分:0)

我建议不要注入代码,而是将数据传输到谷歌电子表格,并通过令人敬畏的表格插件包含地理编码。从那时起,只需将地址放在一列中,然后选择地址列进行地理编码