简体   繁体   English

将Lat Lng从另一个功能推入Street View Image API

[英]Push Lat Lng from another function into Street View Image API

I'm using the Google Maps JavaScript API and this solution for getting closest points on the map and displaying them. 我正在使用Google Maps JavaScript API和此解决方案来获取地图上的最近点并显示它们。

"pt" value example: “ pt”值示例:

38.5803844,-121.50024189999999

Here is the code: 这是代码:

function findClosestN(pt,numberOfResults) {

    var closest = [];

    for (var i=0; i<gmarkers.length;i++) {
         gmarkers[i].distance = google.maps.geometry.spherical.computeDistanceBetween(pt,gmarkers[i].getPosition());
         gmarkers[i].setMap(null);
         closest.push(gmarkers[i]);
       }
       closest.sort(sortByDist);
       return closest;
    }

    function sortByDist(a,b) {
       return (a.distance- b.distance)
    }

    function calculateDistances(pt,closest,numberOfResults) {
      var service = new google.maps.DistanceMatrixService();
      var request =    {
          origins: [pt],
          destinations: [],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL,
          avoidHighways: false,
          avoidTolls: false
        };
      for (var i=0; i<closest.length; i++) request.destinations.push(closest[i].getPosition());
      service.getDistanceMatrix(request, function (response, status) {
        if (status != google.maps.DistanceMatrixStatus.OK) {
          alert('Error was: ' + status);
        } else { 

          var origins = response.originAddresses;
          var destinations = response.destinationAddresses;
          var outputDiv = document.getElementById('search_results');
          outputDiv.innerHTML = '';

          var splitLoc = pt;
          splitLoc.split(",");
          alert(splitLoc[1]);

          photo_lat = splitLoc[0]; // lat
          photo_lng = splitLoc[1]; // lng

          profile_photo = "https://maps.googleapis.com/maps/api/streetview?size=600x300&location=" + photo_lat + "," + photo_lng + "&heading=151.78&pitch=-0.76";

          var results = response.rows[0].elements;
          for (var i = 0; i < numberOfResults; i++) {
            closest[i].setMap(map);
            outputDiv.innerHTML += "<div class='location_list' style='background:url(" + profile_photo + ");'>" + "<div class='inner'>" + "<a href='javascript:google.maps.event.trigger(closest["+i+"],\"click\");'>"+closest[i].title + '</a><br>' + closest[i].address+"<br>"
                + results[i].distance.text + ' appoximately '
                + results[i].duration.text
                + "</div></div>";
          }
        }
      });

}

In the last function, calculateDistances, I am trying to split the coordinates from the "pt" variable and then pass the lat and lng into the street view image api to output a static image of each location. 在最后一个函数calculateDistances中,我试图从“ pt”变量中拆分坐标,然后将经纬度和经度传递到街景图像api中,以输出每个位置的静态图像。

I get the error: 我得到错误:

Uncaught TypeError: splitLoc.split is not a function

when trying to split pt. 尝试分割pt时。 My guess is that pt is not in the right format to be split but I can't figure it out. 我的猜测是pt的格式不正确,但我无法弄清楚。 When I alert pt by itself it displays the lat and lng together correct, but the error is occurring on the split. 当我自行警告pt时,它会同时显示经纬度和经度,但错误发生在拆分时。

How can I split pt into separate lat lng and then pass it in? 如何将pt拆分为单独的lat lng,然后将其传递?

pt is not a string, but an object with properties lat and lng. pt不是字符串,而是具有lat和lng属性的对象。 But if you call alert(pt) it will cast the object as string. 但是,如果您调用alert(pt),它将把对象转换为字符串。

for example 例如

pt.toString() "(37.4418834, -122.14301949999998)" pt.toString()“(37.4418834,-122.14301949999998)”

To get lat/lng just use pt.lat() or pt.lng() 要获取经纬度,只需使用pt.lat()或pt.lng()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM