经典 ASP 类库,用于 Yahoo! Placefinder





5.00/5 (2投票s)
用于调用 Yahoo! Placefinder 进行地理编码的 VBScript 类库
介绍
是否需要制作客户或商店位置的图钉地图? 在您构建地图之前,您需要对地址信息进行地理编码。 地理编码是将文本地址(例如 123 North Burgundy Street, Chicago, IL)转换为经度和纬度点(指定地图网格上的确切点)的过程。 多年来,这是一个昂贵的过程,涉及第三方 GIS 应用程序、季度数据更新以及保持运行所需的高度 IT 专业知识。 感谢 Yahoo! Placefinder 等服务,您每天可以免费对多达 5,000 个地址进行地理编码!
背景
在开始之前,您需要从 Yahoo! Placefinder 申请一个应用程序 ID。 您可以在此处获取 AppID:http://developer.yahoo.com/geo/placefinder/。 将您从 Yahoo! 接收到的应用程序 ID 粘贴到类的 Class_Initialize
部分。 类本身非常简单。 下一部分将展示它有多么容易。
使用代码
将下面的类代码粘贴到您的应用程序中。 要调用地理编码器,首先创建类的实例,然后调用地理编码器。
dim ygeo
dim lon
dim lat
set ygeo = new YGeoCoder
call ygeo.Geocode( address, city, state, zip )
'' now you can get the longitude and latitude from the object
if ygeo.iserror = false then
lon = ygeo.longitude
lat = ygeo.latitude
end if
'' That's it!
在调用 Geocode
后,以下属性将从地理编码器结果中填充
street
- 传递的街道地址;它可能被 Yahoo! 地理编码器更正,并以简写形式表示。city
- 在地理编码时找到的城市(如果只将街道地址和邮政编码传递给地理编码请求)。state
- 在地理编码时找到的州(如果只将街道地址和邮政编码传递给地理编码请求)。zip
- 找到的邮政编码(如果只传递街道地址、城市和州到地理编码请求)。latitude
- 经编码地址的纬度部分。longitude
- 经编码地址的经度部分。country
- 经编码地址的国家/地区。xml
- 从 Yahoo! placefinder 返回的 XML 数据。reason
- 如果地理编码请求失败,这将包含原因。
代码
'' YGeoCoder class
'' by Larry Boeldt
'' (C) 2010 Larry Boeldt
''
'***************************************************************************
' Copyright 2010 Larry Boeldt
' Class: YGeoCoder Version 1.0 11:11 AM Sunday, June 27, 2010
' Author: Larry Boeldt
' Terms:
' GPL, commercial license available
'
' Properties(s):
'
'
' Methods:
' Geocode( address, city, state, zip )
'
' Description:
' A class that wraps most of the functionality of yahoo geocoder.
'
' Setup Instructions:
' Get your appid here: <a href="https://developer.apps.yahoo.com/wsregapp/">https://developer.apps.yahoo.com/wsregapp/</a>
' Copy and paste the licence code in the appid="" assignment
' in class_initialize.
'***************************************************************************
class YGeoCoder
private appid
private m_street
private m_city
private m_state
private m_zip
private m_lon
private m_lat
private m_precision
private m_country
private m_xml
private m_reason
private m_output
private m_url
'***************************************************************************
' Copyright 2010 Larry Boeldt
' Function: class_initialize Version 1.0 11:11 AM Sunday, June 27, 2010
' Author: Larry Boeldt
' Terms:
' GPL, commercial license available
'
' Parameter(s):
' none
'
' Return Value:
' none
'
' Description:
' Initialize the class object with default values.
' Remember to sign up for your yahooappid and paste it below
'***************************************************************************
sub class_initialize()
'' Get your appid here: https://developer.apps.yahoo.com/wsregapp/
'' copy and paste your app id below
appid=""
m_output = "xml"
end sub
property get output()
output = m_output
end property
property get url()
url = m_url
end property
property let output(value)
if value="php" or value="xml" then
m_output=value
else
m_output = "xml"
end if
end property
property get lon()
lon = m_lon
end property
property get lat()
lat = m_lat
end property
property get city()
city = m_city
end property
property get state()
state = m_state
end property
property get zip()
zip = m_zip
end property
property get country()
country = m_country
end property
property get precision()
precision = m_precision
end property
property get address()
address=m_street
end property
property get street()
street = m_street
end property
property get xml()
xml = m_xml
end property
property get reason()
reason = m_reason
end property
'***************************************************************************
' Copyright 2010 Larry Boeldt
' Function: GetPrecision Version 1.0 11:11 AM Sunday, June 27, 2010
' Author: Larry Boeldt
' Terms:
' GPL, commercial license available
'
' Parameter(s):
' Doc XML Document from Yahoo
'
' Return Value:
' Precision value from yahoo
'
' Description:
' Get the precision value returned by yahoo
'
'***************************************************************************
function GetPrecision( doc )
Dim node
dim value
set node=doc.documentElement.selectSingleNode( "/ResultSet/Result" )
if node is nothing then
value = "none"
else
value = node.GetAttribute( "precision" )
end if
set node = nothing
GetPrecision = value
end function
'***************************************************************************
' Copyright 2010 Larry Boeldt
' Function: GetXMLValue Version 1.0 11:11 AM Sunday, June 27, 2010
' Author: Larry Boeldt
' Terms:
' GPL, commercial license available
'
' Parameter(s):
' Doc XML Document from Yahoo
' name the tag name/value you want to retrieve
' Return Value:
' Value of specified tag or blank string
'
' Description:
' Get one of the address return values from the yahoo XML
'
'***************************************************************************
function GetXMLValue( doc, name )
Dim node
dim value
if left(name,1) = "/" then
'' get a custom node
set node=doc.documentElement.selectSingleNode( name )
else
'' get by name from default location
set node=doc.documentElement.selectSingleNode("/ResultSet/Result/" & name )
end if
if node is nothing then
value = ""
else
value = node.text
end if
set node = nothing
GetXMLValue = value
end function
'***************************************************************************
' Copyright 2010 Larry Boeldt
' Function: Geocode Version 1.0 11:11 AM Sunday, June 27, 2010
' Author: Larry Boeldt
' Terms:
' GPL, commercial license available
'
' Parameter(s):
' street street address to geocode
' city city name to geocode
' state state to geoocde
' zip zip code/postal code to geocode
'
' Return Value:
' none
'
' Description:
' Call the Yahoo geocoder with the given address information.
'
'***************************************************************************
function geocode( street, city, state, zip )
dim url
Dim doc
output = m_output
Set doc=CreateObject("Microsoft.XMLDOM")
doc.async=false
call doc.setProperty("ServerHTTPRequest",true)
url="http://local.yahooapis.com/MapsService/V1/geocode" & _
"?appid=" & appid & _
"&street=" & street & _
"&city=" & city & _
"&state=" & state & _
"&zip=" & zip & _
"&output=" & output
m_url = url
doc.load( url )
'' Blank out the property values to defaults before continuing
m_lat = 0.0
m_lon = 0.0
m_street = ""
m_city = ""
m_state = ""
m_zip = ""
m_country = ""
m_xml = "not loaded"
m_precision = "failed"
'' Check if there was an error getting the document from yahoo
if doc.parseError.errorcode=0 then
'' Now check if we have a YAHOO error
if isError( doc ) = true then
m_reason = GetXMLValue( doc, "/Error/Message" )
else
' proceed
m_precision = GetPrecision( doc )
m_lat = GetXMLValue( doc, "Latitude" )
m_lon = GetXMLValue( doc, "Longitude" )
m_street = GetXMLValue( doc, "Address" )
m_city = GetXMLValue( doc, "City" )
m_state = GetXMLValue( doc, "State" )
m_zip = GetXMLValue( doc, "Zip" )
m_country = GetXMLValue( doc, "Country" )
m_reason = ""
end if
m_xml = doc.xml
else
m_reason = doc.parseError.reason & " line: " & doc.parseError.line
m_xml = doc.text
'' Error code here
end if
set doc = nothing
end function
'***************************************************************************
' Copyright 2010 Larry Boeldt
' Function: isError Version 1.0 11:11 AM Sunday, June 27, 2010
' Author: Larry Boeldt
' Terms:
' GPL, commercial license available
'
' Parameter(s):
' Doc XML Document from Yahoo
'
' Return Value:
' True if document is a Yahoo! Error document
' False if document is a valid data packet from yahoo
'
' Description:
' Determine if returned documen is a Yahoo error document
'
'***************************************************************************
function isError( doc )
dim bResult
if GetXMLValue( doc, "/Error/Message" ) = "" then
bResult = false
else
bResult = true
end if
end function
end class '' YGeocoder
工作原理
Geocode
函数是该类中的主要工作核心;它负责组装 URL 并向 Yahoo! Placefinder 服务发出 HTTP GET 调用。 这是完成这项工作的代码段;我已经在每一行添加了注释来描述它所做的事情
Set doc=CreateObject("Microsoft.XMLDOM") '' Create a XML Dom object
doc.async=false '' we will make a syncronus call meaning the object will wait for a response
'' This part is important if you want to call within an ASP page
call doc.setProperty("ServerHTTPRequest",true)
'' Assemble the URL with the parameters passed in the geocode call
url="<a href="http://local.yahooapis.com/MapsService/V1/geocode">http://local.yahooapis.com/MapsService/V1/geocode</a>" & _
"?appid=" & appid & _
"&street=" & street & _
"&city=" & city & _
"&state=" & state & _
"&zip=" & zip & _
"&output=" & output
'' Set a global property m_url with the url string (for debugging purposes)
m_url = url
'' Now make the call to yahoo geocoder service using the assembled URL
doc.load( url )
doc
对象现在将包含来自 Yahoo! Web 服务的 XML 响应。 为了填充对象属性,我解析 XML 文档,查找每个所需的属性值。 为了使这项任务更容易,我使用一个实用函数 GetXMLValue
,它将文档对象和我想捕获其值的返回标签的名称作为参数。 您会看到它是一系列像这样的调用...
m_lat = GetXMLValue( doc, "Latitude" )
在上面的例子中,我获取纬度值并将其赋值给 m_lat
。 这在您的 ygeo
对象中作为属性 .lat
暴露给您的调用代码。 这种做法似乎是多余的,但它是处理类的正确面向对象技术。 以下代码段说明了如何创建该属性
property get lat()
lat = m_lat
end property
GetXMLValue
函数是一种获取所需属性的快捷方式。 您将在下面找到完整的函数,其中逐行注释描述了每一行正在做什么
function GetXMLValue( doc, name )
Dim node '' Placeholder for an XML Node object
dim value '' The value as parsed from the XML node, this is the return value
'' To facilitate "shortcuts" for the specific yahoo code values the following
'' If then statement is used
if left(name,1) = "/" then '' Check if a custom node request is made
'' get a custom node
set node=doc.documentElement.selectSingleNode( name )
else
'' Custom node request was not found, get by name from default location
set node=doc.documentElement.selectSingleNode("/ResultSet/Result/" & name )
end if
'' If the node is nothing (not found) then return a blank value
if node is nothing then
value = ""
else
'' Then node has been found, return the text within the tags of the node
value = node.text
end if
'' Clean up by setting the node to nothing
set node = nothing
'' Return the value of the node (or blank if not found)
GetXMLValue = value
end function
这涵盖了使此类工作的主要函数集。 还有两个更值得注意的函数:isError
和 GetPrecision
。 每个都将 XML 文档对象作为参数,并根据在 XML 中找到的内容返回一个值。 如果 Yahoo! Place Finder 由于某种原因(例如,如果您传递了所有空白值)而无法对地址进行地理编码,则 isError
返回 true
。 GetPrecision
函数返回 Yahoo 的精度值,该值指示经度和纬度值是来自“address
”、“zip
”还是“city
”,具体取决于您的地址信息的匹配准确程度。 在邮政编码和城市的情况下,提供了“centroid
”值。 在大多数情况下,质心是城市或邮政编码统计区域的地理中心。
历史
- 2010/09/06 - 初始文章发布
- 2012/03/06 - 更新代码以适应修订后的 API