我正在传递lat和lng变量,并在div中显示google sreet视图。问题是,当StreetView不可用时,则不会显示任何内容。我想检查给定的lat和lng是否有街景并显示一条消息。下面是我的代码:
var myPano = new GStreetviewPanorama(document.getElementById("street2"), panoOpts);
var location = new GLatLng(lat,lng)
myPano.setLocationAndPOV(location);
也许我应该使用这样的东西:Event.addListener(myPano, "error", errorMessage());
有什么想法吗?
发布于 2011-09-18 06:58:38
在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
}
});
发布于 2016-10-12 18:49:52
谷歌地图JavaScript API v3.25+更新:在v3.25 (当前发布版本)和v3.26 (当前实验版本)中,getPanoramaByLocation()
仍然可用,但not documented anymore。
@arthur-clemens接受的答案仍然有效,但如果你想要更好的兼容性,请使用带有StreetViewLocationRequest
的getPanorama()
:
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
}
});
如果不希望全景搜索仅限于室外全景搜索,请在StreetViewLocationRequest
中忽略source
。
https://stackoverflow.com/questions/2675032
复制相似问题