【问题标题】:How to get POINT OF VIEW of specific Place using Google Maps如何使用谷歌地图获得特定地点的观点
【发布时间】:2019-04-24 07:29:19
【问题描述】:

我想使用 Google Places Autocomplete - Per Session,包括 Place Details,当用户点击某个项目时,显示包含该 Place 的 Google Map StreetView。

我尝试做的方式是:当用户点击自动完成列表时,我使用 getPlace() 函数获取 geometry.coordinates(经纬度),并使用该信息初始化 Google Map Street View。

问题是谷歌地图街景的起始点是随机的。

你知道我该如何解决吗?我想我无法通过 Place Details 获得该信息。有没有其他办法?

谢谢!

【问题讨论】:

标签: google-maps google-places google-street-view


【解决方案1】:

从 Places API 获取地点坐标。

示例:比利时驻巴黎大使馆返回48.87501200000001, 2.2944579999999632

现在使用这些坐标获取街景:

显然,它没有显示比利时大使馆

在全景服务返回数据的时候,你可以得到它的真实坐标,在这种情况下48.87519271414293, 2.294281201461672你可以看到它与地点坐标并不完全相同。 p>

使用几何库,您可以计算全景坐标和您所在位置坐标之间的航向。

let heading = google.maps.geometry.spherical.computeHeading(StreetViewCoords, PlaceCoords);

这会给你一个标题。

现在在显示全景图之前,设置它的标题:

streetView.setPov({
  heading: heading,
  pitch: 0
});

Street View 现在正前往比利时大使馆:

下面的概念证明:

var map;
var panorama;
var panoramaService;
var streetView;
var placeCoords;

function initialize() {

  placeCoords = new google.maps.LatLng(48.87501200000001, 2.2944579999999632);

  map = new google.maps.Map(
    document.getElementById("map-canvas"), {
      zoom: 5,
      center: placeCoords,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

  panoramaService = new google.maps.StreetViewService();

  var panoramaOptions = {
    disableDefaultUI: true
  };

  panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);

  map.setStreetView(panorama);

  streetView = map.getStreetView();
  runPanoramaService();
}

function runPanoramaService() {
  panoramaService.getPanoramaByLocation(placeCoords,
    100,

    function(streetViewPanoramaData, streetViewStatus) {
      if (streetViewStatus == "OK") {

        let heading = google.maps.geometry.spherical.computeHeading(streetViewPanoramaData.location.latLng, placeCoords);

        streetView.setPosition(streetViewPanoramaData.location.latLng);

        streetView.setPov({
          heading: heading,
          pitch: 0
        });

        streetView.setVisible(true);
      }
    });
}

initialize();
#map-canvas {
  height: 80px;
}

#pano {
  height: 160px;
}
<div id="map-canvas"></div>
<div id="pano"></div>

<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initialize">
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2011-01-21
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多