如果我在浏览器中访问您的 URL,您的代码无法正常工作,因为 Google Geocoding API v3 的 URL 不同;
我得到以下回复;
<?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>
元素,因此每次都会失败。
解决方案
根据我用于此确切目的的代码,我对您的源代码进行了一些细微的更改。如果您保持在 google 地图 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
%>