Pregunta

I am currently working with a data listing and one of my columns of data is an address of the listing. I plan on plotting each of my addresses on a Google Map, but I would like to avoid having to manually paste the address into Google to obtain the parameters. I need Longitude, Latitude. My Excel sheet (or I have it in Google Docs spreadsheet) contains about 500 addresses.

So I was hoping that there is someway to do so automatically?

¿Fue útil?

Solución

Courtesy of this great post 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

Otros consejos

You can add a little googl apps script to your google spreadsheet so it will do the job for you. here a demo (add an addresses and it will geocode it for you).
And below the code to perform the geocoding:

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);
  }
}

SmartyStreets offers geocoding of US and international addresses. It's super easy to use - paste your list in their web tool and it returns latitude and longitude within seconds. There are other services that can return lat/long data in similar fashion; I am most familiar with SmartyStreets because I work there.

Also keep in mind that pasting addresses into Google Maps won't necessarily give you accurate information, since Google doesn't validate the address first; SmartyStreets does, which ensures that you're getting data about real locations instead of Google's best guess.

Other solutions you may want to check out: AddressDoctor, Loqate, or other "international address geocoding" providers (a quick Google search will turn up some great options).

Rather than injecting code I would recommend transferring the data over to google spreadsheets and including the geocode by awesome table add-on. From that point simply place the addresses in a single column and select the address column to geocode

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top