この記事は最終更新日から1年以上経過しています。
グーグルストリートビューで、指定した座標が建物上だったり、ストリートビューが提供されていない場所だった場合、そこから一番近い場所のストリートビューを表示する方法があります。
GoogleMap APIのバージョンが異なると書き方が違ってくるので、その違いを紹介。
JavaScriptのクラスとして定義しています。
バージョン2の場合、以下のようにしていました。
4 | latlng: new GLatLng({緯度}, {経度}), |
12 | makeStreetView: function (id) |
14 | stPanorama = new GStreetViewPanorama(document.getElementById(id), this .panoramaOptions); |
16 | stClient = new GStreetviewClient(); |
17 | stClient.getNearestPanoramaLatLng( this .panoramaOptions.position, function (latlng) { |
20 | stPanorama.setLocationAndPOV(latlng); |
バージョン3の場合、以下のような記述になります。
4 | position: new google.maps.LatLng({緯度}, {経度}), |
12 | makeStreetView: function (id) |
14 | stPanorama = new google.maps.StreetViewPanorama(document.getElementById(id), this .panoramaOptions); |
16 | stClient = new google.maps.StreetViewService(); |
17 | stClient.getPanoramaByLocation( this .panoramaOptions.position, 50, function (result, status) { |
18 | if (status == google.maps.StreetViewStatus.OK) |
20 | var nearestPano = result.location.pano; |
21 | var nearestLatLng = result.location.latLng; |
22 | stPanorama.setPosition(nearestLatLng); |
クラスを使用しない場合
2 | position: new google.maps.LatLng({緯度}, {経度}), |
10 | function makeStreetView(id) |
12 | stPanorama = new google.maps.StreetViewPanorama(document.getElementById(id), panoramaOptions); |
14 | stClient = new google.maps.StreetViewService(); |
15 | stClient.getPanoramaByLocation(panoramaOptions.position, 50, function (result, status) { |
16 | if (status == google.maps.StreetViewStatus.OK) |
18 | var nearestPano = result.location.pano; |
19 | var nearestLatLng = result.location.latLng; |
20 | stPanorama.setPosition(nearestLatLng); |