如何检查谷歌街景是否可用并显示消息?

发布于 2024-08-29 22:03:36 字数 387 浏览 5 评论 0原文

我正在传递 lat 和 lng 变量并在 div 中显示 google 街道视图。问题是,当街景不可用时,就不会显示任何内容。我想检查是否有给定纬度和经度的街景并显示一条消息。这是我的代码:

var myPano = new GStreetviewPanorama(document.getElementById("street2"), panoOpts);
var location = new GLatLng(lat,lng)
myPano.setLocationAndPOV(location);

也许我应该使用类似的东西: Event.addListener(myPano, "error", errorMessage());

有什么想法吗?

I am passing lat and lng variables and display google sreet view in a div. The problem is that when the StreetView is unavilable then nothing is displayed. I would like to check if there is a streetview for a given lat and lng and display a message. Here is my code:

var myPano = new GStreetviewPanorama(document.getElementById("street2"), panoOpts);
var location = new GLatLng(lat,lng)
myPano.setLocationAndPOV(location);

Maybe I should use something like: Event.addListener(myPano, "error", errorMessage());

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的她 2024-09-05 22:03:36

在 v3 中,这发生了一些变化。请查看 http://code.google.com/ 上的文档apis/maps/documentation/javascript/reference.html#StreetViewService

更新后的代码是:

var streetViewService = new google.maps.StreetViewService();
var STREETVIEW_MAX_DISTANCE = 100;
var latLng = new google.maps.LatLng(40.7140, -74.0062);
streetViewService.getPanoramaByLocation(latLng, STREETVIEW_MAX_DISTANCE, function (streetViewPanoramaData, status) {
    if (status === google.maps.StreetViewStatus.OK) {
        // ok
    } else {
        // no street view available in this range, or some error occurred
    }
});

In v3 this has changed a bit. Check out the documentation at http://code.google.com/apis/maps/documentation/javascript/reference.html#StreetViewService

The updated code is:

var streetViewService = new google.maps.StreetViewService();
var STREETVIEW_MAX_DISTANCE = 100;
var latLng = new google.maps.LatLng(40.7140, -74.0062);
streetViewService.getPanoramaByLocation(latLng, STREETVIEW_MAX_DISTANCE, function (streetViewPanoramaData, status) {
    if (status === google.maps.StreetViewStatus.OK) {
        // ok
    } else {
        // no street view available in this range, or some error occurred
    }
});
四叶草在未来唯美盛开 2024-09-05 22:03:36

您可能需要查看以下参考资料:

通过目视检查 GStreetviewOverlay 来确定道路是否支持街景视图通常不可行,或者从用户的角度来看也不理想。因此,API 提供了一种以编程方式请求和检索街景数据的服务。通过使用 GStreetviewClient 对象可以促进此服务。

基本上,您可以使用 getNearestPanoramaLatLng() GStreetviewClient 方法a> 类,它将返回一个 GLatLng可以查看街景的最近点的。然后,您可以使用 distanceFrom() 方法检查最近的街道视图点是否距源点在特定阈值内。

这是一个完整的示例,我认为应该是不言自明的:

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps API - Street View Availability</title> 
    <script src="http://maps.google.com/maps?file=api&v=2&sensor=false"
            type="text/javascript"></script> 
  </head> 
  <body> 
    <script type="text/javascript"> 
       var testPoint = new GLatLng(40.7140, -74.0062);   // Broadway, New York
       var svClient = new GStreetviewClient();

       svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
          if ((nearest !== null) && (testPoint.distanceFrom(nearest) <= 100)) {
             alert('Street View Available');             // Within 100 meters
          }
          else {
             alert('Street View Noet Available');        // Not within 100 meters
          }
       });
    </script> 
  </body> 
</html>

You may want to check out the following reference:

Determining whether a road supports Street View by visual inspection of the GStreetviewOverlay is not often feasible, or desirable from a user's perspective. For that reason, the API provides a service which programmatically requests and retrieves Street View data. This service is facilitated through use of the GStreetviewClient object.

Basically you can use the getNearestPanoramaLatLng() method of the GStreetviewClient class, which will return you a GLatLng of the nearest point where street view is available. You can then use the distanceFrom() method to check if the nearest street view point is within a certain threshold from your source point.

Here is a full example, which I believe should be self explanatory:

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps API - Street View Availability</title> 
    <script src="http://maps.google.com/maps?file=api&v=2&sensor=false"
            type="text/javascript"></script> 
  </head> 
  <body> 
    <script type="text/javascript"> 
       var testPoint = new GLatLng(40.7140, -74.0062);   // Broadway, New York
       var svClient = new GStreetviewClient();

       svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
          if ((nearest !== null) && (testPoint.distanceFrom(nearest) <= 100)) {
             alert('Street View Available');             // Within 100 meters
          }
          else {
             alert('Street View Noet Available');        // Not within 100 meters
          }
       });
    </script> 
  </body> 
</html>
反话 2024-09-05 22:03:36

Google Maps JavaScript API v3.25+ 更新:
在 v3.25(当前发布版本)和 v3.26(当前实验版本)中,getPanoramaByLocation() 仍然可用,但 不再记录

@arthur-clemens 接受的答案仍然有效,但如果您想要更好的兼容性,请使用 getPanorama()StreetViewLocationRequest 代替:

var gstService = new google.maps.StreetViewService();

gstService.getPanorama({
    location: new google.maps.LatLng(40.7140, -74.0062),
    source: google.maps.StreetViewSource.OUTDOOR
}, function (data, status) {
    if (status === google.maps.StreetViewStatus.OK) {
        // OK
    } else {
        // error or no results
    }
});

source 中省略 source code>StreetViewLocationRequest 如果您不希望全景搜索仅限于室外。

Update for Google Maps JavaScript API v3.25+:
In v3.25 (current release version) and v3.26 (current experimental version), getPanoramaByLocation() is still available but not documented anymore.

@arthur-clemens's accepted answer still works, but use getPanorama() with a StreetViewLocationRequest instead if you want better compatibility:

var gstService = new google.maps.StreetViewService();

gstService.getPanorama({
    location: new google.maps.LatLng(40.7140, -74.0062),
    source: google.maps.StreetViewSource.OUTDOOR
}, function (data, status) {
    if (status === google.maps.StreetViewStatus.OK) {
        // OK
    } else {
        // error or no results
    }
});

Omit source in the StreetViewLocationRequest if you do not want the panorama search to be limited to outdoor ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文