问题描述
我使用Google Maps JavaScript API和此解决方案在地图上获取最近的点并显示它们。 值例如: 以下是代码: 在最后一个函数calculateDistances中,我试图分割从pt变量的坐标,然后将lat和lng传递到街景视图图像api中,以输出每个位置的静态图像。 我得到错误: 尝试分割点时。我的猜测是,pt格式不对,但我无法弄清楚。当我自己提醒pt时,它显示lat和lng在一起是正确的,但错误发生在分割上。 如何将pt分成单独的经纬度,然后传递它? pt不是一个字符串,而是一个具有属性lat和lng的对象。但是,如果您调用alert(pt),它会将该对象转换为字符串。 pt。 toString() 获取纬度/经度只需使用pt.lat或pt.lng I'm using the Google Maps JavaScript API and this solution for getting closest points on the map and displaying them. "pt" value example: Here is the code: 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: 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? 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 这篇关于将Lat Lng从另一个功能推入Street View Image API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
$ b $ p
38.5803844,-121.50024189999999
函数findClosestN(pt,numberOfResults){
var closest = [];
for(var i = 0; i gmarkers [i] .distance = google.maps.geometry.spherical.computeDistanceBetween(pt,gmarkers [i ] .getPosition());
gmarkers [i] .setMap(null);
closest.push(gmarkers [i]);
}
closest.sort(sortByDist);
回报最接近;
函数sortByDist(a,b){
return(a.distance- b.distance)
}
函数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 ++){
接近的[I] .setMap(地图);
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>;
}
}
});
$ b $ p
$ b
Uncaught TypeError:splitLoc.split不是函数
(37.4418834,-122.14301949999998)
38.5803844,-121.50024189999999
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>";
}
}
});
}
Uncaught TypeError: splitLoc.split is not a function