【问题标题】:Facing the targeted building with Google StreetView使用 Google StreetView 面向目标建筑
【发布时间】:2012-09-11 20:02:23
【问题描述】:

我的问题很简单。

我需要heading 值来了解如何定位 POV。

sv.getPanoramaByLocation() 在这种情况下返回一个data 变量,其中包含两个箭头的heading,您可以朝哪个方向走得更远。

但是,它并没有给我heading 值以了解以哪种方式查看建筑物。 但是可以在街景中使用标记来定位您的建筑物! example

谁能帮我解决这个问题?我可以做任何你想要的垃圾。

【问题讨论】:

    标签: php google-maps-api-3 google-street-view


    【解决方案1】:

    对您要“查看”的建筑物的地址进行地理编码。使用geometry library computeHeading(from:LatLng, to:LatLng) 函数计算街景位置和建筑物之间的航向。

    (假设地理编码器返回“屋顶”地理编码)

    example (Statue of Liberty)

    另一种选择,使用路线服务:

    related question: Request main road StreetView panoramas instead of back alleys from API

    代码 sn-p 使用路线服务获取道路上的位置以用于街景“相机”位置(现在可以更好地返回“内部”街景位置):

    var map;
    var berkeley = new google.maps.LatLng(37.869085, -122.254775);
    var sv = new google.maps.StreetViewService();
    var geocoder = new google.maps.Geocoder();
    var directionsService = new google.maps.DirectionsService();
    var panorama;
    var myLatLng;
    var address = "525 Beacon St. Boston, MA";
    
    function initialize() {
    
      panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"));
    
    
      directionsService.route({
        origin: address,
        destination: address,
        travelMode: google.maps.TravelMode.DRIVING
      }, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          // myLatLng = response.routes[0].legs[0].start_location;
          sv.getPanoramaByLocation(response.routes[0].legs[0].start_location, 50, processSVData);
    
          var marker = new google.maps.Marker({
          position: response.routes[0].legs[0].start_location,
          map: map,
          title: "Directions"
        });
          map.setCenter(myLatLng);
    
    } else document.getElementById('info').innerHTML += "status:"+status+"<br>";
      });
    
      geocoder.geocode({
        'address': address
      }, geocoderCallback);
      
      // Set up the map
      var myOptions = {
        zoom: 15
      };
    
      map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    
    function processSVData(data, status) {
      if (status == google.maps.StreetViewStatus.OK) {
    
        panorama.setPano(data.location.pano);
        var camera = new google.maps.Marker({
          position: data.location.latLng,
          map: map,
          draggable: true,
          title: "camera"
        });
        var heading = google.maps.geometry.spherical.computeHeading(data.location.latLng, myLatLng);
        document.getElementById('info').innerHTML += "heading:"+heading+"<br>"
        + "location: "+myLatLng.toUrlValue(6)+"<br>"
        + "camera:"+data.location.latLng.toUrlValue(6)+"<br>";
        
        
        // alert(data.location.latLng+":"+myLatLng+":"+heading);
        panorama.setPov({
          heading: heading,
          pitch: 0,
          zoom: 1
        });
        panorama.setVisible(true);
      } else {
        alert("Street View data not found for this location.");
      }
    }
    
    function geocoderCallback(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        myLatLng = results[0].geometry.location;
        map.setCenter(myLatLng);
        if (results[0].geometry.viewport) map.fitBounds(results[0].geometry.viewport);
        else if (results[0].geometry.bounds) map.fitBounds(results[0].geometry.bounds);
        else map.setZoom(15);
        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          title: address
        });    
    
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    };
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    #map_canvas {
      height: 100%;
    }
    <script src="http://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
    
    <div id="pano" style="width: 425px; height: 400px;float:left"></div>
    <div id="info"></div>
    
    <div id="map_canvas" style="width: 425px; height: 400px;float:left"></div>
    <div id="map_center"></div>
    <div id="streetview_pov"></div>

    【讨论】:

    • google.maps.geometry.spherical.computeHeading(); 是一个完美的解决方案,谢谢!
    • 我如何获得“StreetView”位置?我有地理编码地址的 lat & lng,但我不知道要发送什么 OTHER LatLng 到 computeHeading();
    • 全景图的位置
    猜你喜欢
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2016-04-20
    • 2019-01-22
    • 2018-09-22
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    相关资源
    最近更新 更多