Push Lat Lng from another function in the Street View API

I use the Google Maps JavaScript API and this solution is to get the nearest points on the map and display them.

"pt" example value:

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 try to separate the coordinates from the variable "pt" and then pass lat and lng to the api image to view the streets to display a static image of each location.

I get an error message:

 Uncaught TypeError: splitLoc.split is not a function 

when trying to smash pt. I assume pt is not in the correct format to be split, but I cannot figure it out. When I warn pt on its own, it displays lat and lng together correctly, but the error occurs on a split.

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

0
source share
1 answer

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

eg

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

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

+1
source

Source: https://habr.com/ru/post/1487930/


All Articles