如何在Classic ASP中使用Geocoding API v3

时间:2014-03-26 19:13:15

标签: xml google-maps-api-3 asp-classic geocoding

想知道是否有人可以帮助我。我正在尝试返回地址的lat和lng的结果。新的编码和卡住了。以下代码正常工作,直到Geocoding从v2转到v3。你能告诉我哪里出错了吗?我需要一个新的v3键或键吗?提前谢谢。

<%
Function GetXML(url)
Dim xmlobj
Set xmlobj = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlobj.setTimeouts 30000, 30000, 30000, 30000
xmlobj.Open "GET", url, False
xmlobj.send()
If xmlobj.status = 200 Then
    GetXML = xmlobj.responseXML.xml
Else
    Response.Write "The geocoding server could not be reached."
    Response.End
End If
End Function

address = "30 Dixon, EN76HA"


url="http://maps.google.com/maps/geo?output=xml&key=XXXXXXX &q="&Server.URLEncode(address)






Response.Write "You entered: "&address&"<br />"

xml = GetXML(url) 'Function to return raw XML as text

if InStr(xml,"<coordinates>")>0 then
coords = split(xml,"<coordinates>") 'Get everything after the opening coordinates tag
coords2 = split(coords(1),"</coordinates>") 'Get everything before the ending coordinates tag
coordinatesSplit = split(coords2(0),",") 'split it at the commas
lng = coordinatesSplit(0) 'The first value is the longitude
lat = coordinatesSplit(1) 'The second value is the latitude

Response.Write "The geo-coded coordinates are: "&lat&" "&lng
else
'No coordinates were returned
Response.Write "The address could not be geocoded."
Response.End
end if
%>

1 个答案:

答案 0 :(得分:4)

您的代码无效,因为如果我在浏览器中访问您的网址,Google地理编码API v3的网址会有所不同;

  

http://maps.google.com/maps/geo?output=xml&q=30%20Dixon,%20EN76HA

我收到以下回复;

<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0">
  <Response>
    <Status>
      <code>610</code>
      <request>geocode</request>
      <error_message>The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/</error_message>
    </Status>
  </Response>
</kml>

您的代码永远不会找到<coordinates>元素,因此每次都会失败。


解决方案

根据我用于此目的的代码,我对您的来源做了一些小改动。如果您保持在谷歌地图api设置的使用限制范围内,您实际上不需要传递密钥,但如果您已经拥有api密钥,则只需将其添加到url变量中。

<%
Function GetXML(addr)
  Dim objXMLDoc, url, docXML, lat, lng, mapref

  'URL for Google Maps API - Doesn't need to stay here could be stored in a 
  'config include file or passed in as a function parameter.
  url = "http://maps.googleapis.com/maps/api/geocode/xml?address={addr}&sensor=false"
  'Inject address into the URL
  url = Replace(url, "{addr}", Server.URLEncode(addr))

  Set objXMLDoc = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
  objXMLDoc.setTimeouts 30000, 30000, 30000, 30000
  objXMLDoc.Open "GET", url, False
  objXMLDoc.send()

  If objXMLDoc.status = 200 Then
    Set docXML = objXMLDoc.responseXML
    'Check the response for a valid status
    If UCase(docXML.documentElement.selectSingleNode("/GeocodeResponse/status").Text) = "OK" Then
      lat = docXML.documentElement.selectSingleNode("/GeocodeResponse/result/geometry/location/lat").Text
      lng = docXML.documentElement.selectSingleNode("/GeocodeResponse/result/geometry/location/lng").Text
      'Create array containing lat and long
      mapref = Array(lat, lng)
    Else
      mapref = Empty
    End If
  Else
    mapref = Empty
  End If

  'Return array
  GetXML = mapref
End Function

Dim coords, address

address = "30 Dixon, EN76HA"
coords = GetXML(address)
'Do we have a valid array?
If IsArray(coords) Then
  Response.Write "The geo-coded coordinates are: " & Join(coords, ",")
Else
  'No coordinates were returned
  Response.Write "The address could not be geocoded."
End If
%>