Javascript 零状态结果谷歌地图地理编码状态
我们需要在谷歌地图中的某些点之间绘制方向。我们尝试过,但方向状态是“零结果”。我们尝试过这样做Javascript 零状态结果谷歌地图地理编码状态,javascript,google-maps,cordova,google-maps-api-3,geolocation,Javascript,Google Maps,Cordova,Google Maps Api 3,Geolocation,我们需要在谷歌地图中的某些点之间绘制方向。我们尝试过,但方向状态是“零结果”。我们尝试过这样做 function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); var chicago = new google.maps.LatLng(41.850033, -87.6500523); var mapOptions = { zoom: 6, center: chicago
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 6,
center: chicago
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
debugger;
var waypts = [];
var checkboxArray = document.getElementById('waypoints');
for (var i = 0; i < myjson.length; i++) {
var data = myjson[i];
waypts.push({
location: new google.maps.LatLng(parseFloat(myjson[i].Lattitude), parseFloat(myjson[i].Longitude)),
stopover: true
});
}
debugger;
console.log(waypts);
var request = {
origin:waypts[0].location,
destination:waypts[waypts.length-1].location,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
debugger;
directionsService.route(request, function(response, status) {
debugger;
if (status == google.maps.DirectionsStatus.OK) {
debugger;
directionsDisplay.setDirections(response);
var route = response.routes[0];
}
});
}
从输出来看,您的myjson(数组?)似乎有问题 不管怎样,当我试图复制它时,它是有效的
//from how you use your myjson var, I would assume it looks something like this:
var myjson = [
{ Lattitude: 37.4184, Longitude: -122.0880 },
{ Lattitude: 37.7833, Longitude: -122.4167 },
{ Lattitude: 38.5539, Longitude: -121.7381 }
]
var waypts = [];
for (var i = 0; i < myjson.length; i++) {
var data = myjson[i];
waypts.push({
location: new google.maps.LatLng(parseFloat(myjson[i].Lattitude), parseFloat(myjson[i].Longitude)),
stopover: true
});
}
console.log(JSON.stringify(waypts));
var request = {
origin:waypts[0].location,
destination:waypts[waypts.length-1].location,
waypoints: waypts.slice(1, waypts.length-1),
//the example in the documentation does not include the start & end points
//that might be a problem https://developers.google.com/maps/documentation/javascript/directions#DirectionsRequests
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
希望这有帮助在我的例子中,数据来自HTML
li
元素,但它是以字符串的形式出现的,即
coordsData = {
lat: {
value: "19.4921383",
someOtherProperties: {}
},
lng: {
value: "-99.04651999999999",
someOtherProperties: {}
}
}
因此,我必须将这些值转换为:
var myCoords = new google.maps.LatLng(parseFloat(coordsData.lat.value), parseFloat(coordsData.lng.value))
以防万一有人遇到同样的问题。当你向它发出警报(或执行
console.log()
)时,你的waypts
看起来是什么样子?@duncan waypts像这样0:Object location:kf D:78.549404299999k:17.4211137 proto:kf中途停留:true proto:Object 1:Object 2:Object 3:Object uh。。。看起来不像一个数组,但很难从评论中分辨出来。如果执行console.log(),您能准确地更新您的答案吗?@duncan Yes我们更新代码jest我们打印航路点
coordsData = {
lat: {
value: "19.4921383",
someOtherProperties: {}
},
lng: {
value: "-99.04651999999999",
someOtherProperties: {}
}
}
var myCoords = new google.maps.LatLng(parseFloat(coordsData.lat.value), parseFloat(coordsData.lng.value))