0

I'm using the Google Maps JavaScript API and this solution for getting closest points on the map and displaying them.

"pt" value example:

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.

I get the error:

Uncaught TypeError: splitLoc.split is not a function

when trying to split pt. My guess is that pt is not in the right format to be split but I can't figure it out. When I alert pt by itself it displays the lat and lng together correct, but the error is occurring on the split.

How can I split pt into separate lat lng and then pass it in?

Community
  • 1
  • 1
cpcdev
  • 1,130
  • 3
  • 18
  • 45

1 Answers1

2

pt is not a string, but an object with properties lat and lng. But if you call alert(pt) it will cast the object as string.

for example

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

To get lat/lng just use pt.lat() or pt.lng()

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Vedran Jukic
  • 841
  • 8
  • 14