如何使用经典ASP地理编码API第3版 [英] How to use Geocoding API v3 in Classic ASP

查看:96
本文介绍了如何使用经典ASP地理编码API第3版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人能帮助我。我试图返回的纬度和地址LNG的结果。新的编码,并卡住了。下面code工作得很好,直到地理编码从V2至V3去了。你能告诉我,我要去哪里错了吗?而且我需要在所有新V3按键或钥匙吗?先谢谢了。

<%
功能的getXML(URL)
昏暗xmlobj
设置xmlobj =的Server.CreateObject(MSXML2.ServerXMLHTTP.3.0)
xmlobj.setTimeouts 30000,30000,30000,30000
xmlobj.OpenGET,网址,假
xmlobj.send()
如果xmlobj.status = 200然后
    的getXML = xmlobj.responseXML.xml
其他
    RESPONSE.WRITE地理编码服务器可能无法达成。
    到Response.End
万一
结束功能地址=30迪克森,EN76HA
URL =htt​​p://maps.google.com/maps/geo?output=xml&key=XXXXXXX&安培; Q =&放大器; Server.URLEn code(地址)
的Response.Write您输入:&放大器;地址&安培;< BR />中XML =的getXML(URL)函数返回原始的XML文本如果INSTR(XML,&所述;坐标>中)大于0则
COORDS =拆分(XML,<&坐标GT;)'获取一切开幕坐标标记之后
coords2 =拆分(COORDS(1),< /坐标>)'获取一切结束坐标标记之前
coordinatesSplit =拆分(coords2(0),)'在逗号分割它
经度= coordinatesSplit(0)'的第一个值是经度
纬度= coordinatesSplit(1)第二个值是纬度的Response.Write的地理codeD坐标是:&放大器;纬度和放大器;&放大器;液化天然气
其他
'被退回无坐标
RESPONSE.WRITE的地址无法进行地理codeD。
到Response.End
万一
%GT;


解决方案

您code不工作,因为URL是针对谷歌地理编码API第3版不同的,如果我去浏览器的URL;


  

http://maps.google.com ?/地图/地理输出= XML和放大器; q = 30%20Dixon,%20EN76HA


我获得以下响应;

<?XML版本=1.0编码=UTF-8&GT?;
< KML的xmlns =htt​​p://earth.google.com/kml/2.0>
  <应变及GT;
    <状态>
      < code取代; 610 LT; / code>
      <请求>地理code< /请求>
      &LT; ERROR_MESSAGE&gt;在地理编码API V2已经推掉9月9日,2013年的地理编码API V3现在应该使用。了解更多https://developers.google.com/maps/documentation/geocoding/</error_message>
    &LT; /状态&gt;
  &LT; /响应&GT;
&LT; / KML&GT;

您code永远不会找到&LT;坐标方式&gt; 元素等会失败每次


解决方案

在code和我已经用我做了你的源代码一些小的改动这个确切的目的为主。你实际上并不需要的,如果你留在使用的限制由谷歌地图API建立在传递一个关键,但如果你有一个API密钥已经然后只需将其添加到网​​址变量。

&LT;%
功能的getXML(地址)
  昏暗objXMLDoc,网址,docXML,纬度,经度,MA preF  网址为谷歌地图API - 不需要留在这儿可以存储在一个
  配置包含文件或传递作为函数参数。
  URL =htt​​p://maps.googleapis.com/maps/api/geo$c$c/xml?address={addr}&sensor=false
  注入地址到URL
  URL =替换(URL,{}地址,Server.URLEn code(地址))  设置objXMLDoc =的Server.CreateObject(MSXML2.ServerXMLHTTP.3.0)
  objXMLDoc.setTimeouts 30000,30000,30000,30000
  objXMLDoc.OpenGET,网址,假
  objXMLDoc.send()  如果objXMLDoc.status = 200然后
    设置docXML = objXMLDoc.responseXML
    检查有效状态的响应
    如果用Ucase(docXML.documentElement.selectSingleNode(/地理位置codeResponse /状态)。文本)=确定然后
      纬度= docXML.documentElement.selectSingleNode(/地理位置codeResponse /结果/几何/位置/纬度)。文本
      LNG = docXML.documentElement.selectSingleNode(/地理位置codeResponse /结果/几何/位置/ LNG)。文本
      创建一个包含经纬度和多头排列
      马preF =阵列(纬度,经度)
    其他
      马preF =空
    万一
  其他
    马preF =空
  万一  '返回数组
  的getXML = MA preF
结束功能昏暗COORDS,地址地址=30迪克森,EN76HA
COORDS =的getXML(地址)
'我们有一个有效的数组?
如果IsArray的(COORDS)然后
  的Response.Write的地理codeD坐标是:&放大器;加入(COORDS,,)
其他
  '被退回无坐标
  RESPONSE.WRITE的地址无法进行地理codeD。
万一
%GT;

wondering if anyone could help me. I'm trying to return the results of lat and lng of an address. New to coding and have got stuck. The following code worked fine until the Geocoding went from v2 to v3. Can you tell me where I'm going wrong? and do I need a new v3 key or key at all? Thanks in advance.

<%
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
%>

解决方案

Your code is not working because the URL is different for the Google Geocoding API v3, if I go to your URL in browser;

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

I get the following response;

<?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>

Your code will never find the <coordinates> element and so will fail every time.


The solution

Based on code I've used for this exact purpose I've made a few minor changes to your source. You don't actually need to pass a key if you stay within the limits on usage set up by the google maps api but if you have a api key already then just add it into the url variable.

<%
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
%>

这篇关于如何使用经典ASP地理编码API第3版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆