我想使用另一个数组在 java 脚本数组中插入几个位置
I want to insert few location in to java script array using a another array
此函数获取几个位置之间的总距离。通常它适用于这个数组,而不遵循getLocations数组和for循环。
waypoints: [{location: 'Galle'},{location: 'Kandy'},{location: 'minneriya wildlife park'}, {location: 'Horton Plains'}],
现在我想使用 for 循环将这些位置插入到 getLocations 数组中,如下所示:
function getDistance() {
var getLocations = ['Galle','Kandy', 'minneriya wildlife park', 'Horton Plains'];
var request = {
origin: getLocations[0],
destination: getLocations[0],
for (var i = 0; i < getLocations.length; i++) {
waypoints = [{location :getLocations[i]}]
}
waypoints,travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
现在它无法正常工作。我在网上冲浪寻找解决方案,但找不到。请帮助我。我想知道它是如何以正确的方式工作的。谢谢
您的示例包含错误,特别是在初始化对象的行request
waypoints
数组无法以这种方式初始化。一种选择是将waypoints
数组初始化置于对象初始化之外request
如下所示。
固定示例
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 8
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
getDistance();
directionsDisplay.setMap(map);
}
function getDistance() {
var locations = ['Galle', 'Kandy', 'minneriya wildlife park', 'Horton Plains'];
var waypoints = locations.map(function(loc) {
return { 'location': loc };
});
var request = {
origin: locations[0],
destination: locations[0],
waypoints: waypoints,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<div id="map-canvas"></div>
相关文章:
- 将数组中的值插入到表中
- 在数组中插入对象失败
- JavaScript-在不影响索引的情况下将元素插入数组
- 如何使用JQuery将值数组插入到数据库中
- 通过node-js脚本将文档数组插入mongodb中的数据库时出现问题
- 将 json_encode() 数组插入 javascript 函数
- 如何在 zend_form 中将数组插入 JavaScript
- 将 JavaScript 数组插入到 SQL 表中
- Javascript 数组插入
- 在for循环javascript中将数组插入到空数组中
- 将数组插入location.href
- 关于将javascript中的数组插入HTML中的段落元素的基本帮助
- 如何通过angular js将数组插入到node js web服务中
- AngularJS将数组插入指令中的字符串
- 通过javascript将php值的数组插入到select选项中
- 根据数组插入html
- 如何将哈希映射数组插入Redis?(Node.js)
- 高图表将数据从数组插入到折线图中
- JavaScript如何将数组插入到表中
- 在jquery/js中从数组插入元素到表