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];
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?
source share