对于我的项目,我需要对一组我想知道 GPS 坐标的位置进行地理编码。
手动操作的位置数量太大,但不会太多,这样我就不会遇到谷歌对使用地理编码 API 的限制的问题。
对我来说最方便的方法是使用 OpenOffice Calc。
我找到了一个VBA 代码,它可以满足我的需要:
Function GetGeoData(sSearch as String) as String
If Len(sSearch) = 0 Then Exit Function 'we dont need empty cells <img draggable="false" class="emoji" alt="" src="http://s.w.org/images/core/emoji/72x72/1f609.png">
URL = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=true&address=" 'we will use the google maps api
URL = URL & sSearch 'create the searchstring
oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" ) 'this is the Sefvice in getting the data from the web
On Error GoTo ErrorResponse
oInputStream = oSimpleFileAccess.openFileRead(URL) 'use the URL
oTextStream = createUnoService("com.sun.star.io.TextInputStream") 'get the data from the web
oTextStream.InputStream = oInputStream 'this is the data
aDelimiters = Array(ASC(">"),ASC("<")) 'as the stream is segmented with ">" and "<"
sLastString = ""
Do While NOT oTextStream.isEOF 'go through the google output
sThisString = oTextStream.readString(aDelimiters,True)
Select Case sLastString 'now search for the entries
Case "lat": 'latitudes
sLat = sThisString
Case "lng": 'longitude
sLon = sThisString
End Select
sLastString = sThisString
Loop
GetGeoData = " Longitude: " & sLon & " Latitude: " &sLat 'this is our output in the new cell
oInputStream.closeInput()
Exit Function
ErrorResponse:
GetGeoData = "no values found!!!"
End Function
然而,虽然对于确切的地址来说很好,但当涉及到谷歌称为多边形的定居点时,就会出现问题。在这种情况下,代码只保留它在 xml 信息中找到的最后一组坐标,但这对应于多边形的东北角。我很高兴拥有多边形的中心,它对应于谷歌地图生成的 xml 文档中的第一组坐标。
- 谁能向我解释如何根据此代码在 xml 文件中选择特定节点?
- 另一种解决方案是只保留第一组坐标。