街道视图客户端查询
通过在视觉上检查 GStreetviewOverlay
的方式,来确定道路是否支持街道视图通常不太可行,从用户角度而言也不可取。出于此原因,API 提供了可以程序方式发出请求并检索街道视图数据的一种服务。此服务通过使用 GStreetviewClient
对象得到简化。
执行街道视图查找
GStreetviewClient
对象使用 Google 的街道视图服务执行全景数据查找。由于此种查找为异步进行,所以此类方法需要在接收数据时执行回调函数。所有已给出的回调函数在没有任何值返回的情况下都将传递 null
,因此您应在回调函数中检查这种情况。
GStreetviewClient
方法 getNearestPanoramaLatLng()
检索与一个给定位置 (其本身作为 GLatLng
传递)邻近的全景图像的 GLatLng
。
getNearestPanorama()
和 getPanoramaById()
都替代检索 GStreetviewData
对象,该对象存储关于特定全景对象的元数据。此类数据在下节中介绍。
处理客户端响应
GStreetviewData
对象的结构包括三个属性:location
和 copyright
(包含关于所显示的特定图像的信息),以及 links
(提供关于邻近全景对象的信息)这些属性的结构如下所述:
# The location property uses the GStreetviewLocation object literal location: { latlng: GLatLng, pov: { yaw: String, pitch: String, zoom: String }, description: String, panoId: String } copyright: String # The links property uses the GStreetviewLink object literal links[]: { yaw: String, description: String, panoId: String }
(关于 GStreetviewLocation
和 GStreetviewLink
object literal 的完整描述 在地图 API 参考中显示。)
注意: 不应将 GStreetviewData.location
属性与 window.location
属性混淆。如果尝试从此对象的 location
属性中抽取数据,请确保您确实接收到了从街道视图服务器传回的响应(见下文)。否则,location
属性将默认为 window.location
,并且将发生意外行为。
如果对 GStreetviewClient
对象的请求成功,它会将 GLatLng
或 GStreetviewData
对象传递给指定的回调函数。原因是检索街道视图数据为异步进行,然而,客户端对象可能不检索这些数据对象,因此您的代码不应依赖于当前显示的对象。相反,您应始终检查确信有返回值的任何请求所返回的 code
值。以下代码片段说明了此概念。
panoClient = new GStreetviewClient(); panoClient.getPanoramaById(panoData.location.panoId, processReturnedData); function processReturnedData(panoData) { if (panoData.code != 200) { GLog.write('showPanoData: Server rejected with code: ' + panoData.code); return; } // Code to actually process the GStreetviewData object is contained here }
包含 GStreetviewData
样本结构的完整响应如下所示:
{ location: { latlng: "42.345566, -71.098354" pov: { yaw: "370.64659986187695" pitch: "-20" zoom: "1" } description: "Yawkey Way" panoId: "-KNGDaZvSQjMqug7ISM_CA" } copyright: "© 2008 Google" links:[ { yaw: "0" description: "Yawkey Way" panoId: "S142iWXa_4Fi7L7d8HKhuQ" }, { yaw: "0" description: "Yawkey Way" panoId: "2vFI79AjOpHTAYJSCKquFg" } ] }
以下样本应用程序将显示初始全景对象,抽取其 ID,然后存储返回的 GStreetviewData
对象中的链接全景对象,并显示与该街道视图对象相关的数据集。用户每次单击“下一步”时,该过程都会重复,允许用户“行进”过一组邻近全景对象。
var map; var myPano; var panoClient; var nextPanoId; function initialize() { var fenwayPark = new GLatLng(42.345573,-71.098326); var fenwayPOV = {yaw:370.64659986187695,pitch:-20}; panoClient = new GStreetviewClient(); map = new GMap2(document.getElementById("map_canvas")); map.setCenter(fenwayPark, 15); GEvent.addListener(map, "click", function(overlay,latlng) { panoClient.getNearestPanorama(latlng, showPanoData); }); myPano = new GStreetviewPanorama(document.getElementById("pano")); myPano.setLocationAndPOV(fenwayPark, fenwayPOV); GEvent.addListener(myPano, "error", handleNoFlash); panoClient.getNearestPanorama(fenwayPark, showPanoData); } function showPanoData(panoData) { if (panoData.code != 200) { GLog.write('showPanoData: Server rejected with code: ' + panoData.code); return; } nextPanoId = panoData.links[[0[].panoId; var displayString = [[ "Panorama ID: " + panoData.location.panoId, "LatLng: " + panoData.location.latlng, "Copyright: " + panoData.copyright, "Description: " + panoData.location.description, "Next Pano ID: " + panoData.links[[0[].panoId [].join(" "); map.openInfoWindowHtml(panoData.location.latlng, displayString); GLog.write('Viewer moved to' + panoData.location.latlng); myPano.setLocationAndPOV(panoData.location.latlng); } function next() { // 取得下一个panoId // 请注意,这个写法并不准确,当移到最后时就不行了。 panoClient.getPanoramaById(nextPanoId, showPanoData); } function handleNoFlash(errorCode) { if (errorCode == 603) { alert("错误:您的浏览器似乎不支持 Flash"); return; } }
http://code.google.com/intl/zh-CN/apis/maps/documentation/examples/streetview-data.html