将Javascript对象数组转换为google.maps.LatLng对象数组
如何将传递到JavaScript函数中的对象数组转换为适合构建JavaScript函数的将Javascript对象数组转换为google.maps.LatLng对象数组,javascript,arrays,geojson,Javascript,Arrays,Geojson,如何将传递到JavaScript函数中的对象数组转换为适合构建JavaScript函数的google.maps.LatLng对象的普通数组。我试图从传递给JavasSript函数的lat/lnt对象数组中初始化一个GoogleMap实例。查看我的JavaScript控制台(在Vhrome浏览器中),我可以看到我成功地接收了66个对象的数组 下面的var updateMap=function(flightPlan)是我遇到问题的地方 <script> // This examp
google.maps.LatLng
对象的普通数组。我试图从传递给JavasSript函数的lat/lnt对象数组中初始化一个GoogleMap实例。查看我的JavaScript控制台(在Vhrome浏览器中),我可以看到我成功地接收了66个对象的数组
下面的var updateMap=function(flightPlan)
是我遇到问题的地方
<script>
// This example creates a 2-pixel-wide red polyline showing the path of
// the first trans-Pacific flight between Oakland, CA, and Brisbane,
// Australia which was made by Charles Kingsford Smith.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: { lat: 0, lng: -180 },
mapTypeId: 'terrain'
});
var flightPlanCoordinates = [
{ lat: 37.772, lng: -122.214 },
{ lat: 21.291, lng: -157.821 },
{ lat: -18.142, lng: 178.431 },
{ lat: -27.467, lng: 153.027 }
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
// function to initialize google map with a flight plan
// effectively an array of waypoints containing
// latitude, longitude and bearing. The flightPlan below
// is an array of json objects.
var updateMap = function (flightPlan) {
// waypoint data is always a list of nulls
console.log(flightPlan);
// how do I create an array similar to
// var flightPlanCoordinates above suitable
// for initializing the google map
var latLngArray;
// how do I convert to suitable lat/lng array
//waypointData.forEach(function(next)){
// latLngArray.push(new google.maps.LatLng(
// next.lat, next.lng));
//}
// waypoint data is always a list of nulls
console.log(latLngArray);
}
// script called once web page loaded
window.onload = function () {
new QWebChannel(qt.webChannelTransport,
function (channel) {
console.log(channel.objects);
// whenever the route data changes, invoke updateMap slot
var dataSource = channel.objects.routeIPC;
dataSource.routesChanged.connect(updateMap);
}
);
}
}
</script>
//此示例创建一条2像素宽的红色多段线,显示
//加利福尼亚州奥克兰和布里斯班之间的第一次跨太平洋航班,
//澳大利亚,由查尔斯·金斯福德·史密斯制作。
函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
缩放:3,
中心:{lat:0,lng:-180},
mapTypeId:'地形'
});
var FlightPlan坐标=[
{拉丁美洲:37.772,液化天然气:-122.214},
{lat:21.291,lng:-157.821},
{拉丁美洲:-18.142,液化天然气:178.431},
{拉丁美洲:-27.467,液化天然气:153.027}
];
var flightPath=new google.maps.Polyline({
路径:FlightPlan坐标,
测地线:正确,
strokeColor:“#FF0000”,
笔划不透明度:1.0,
冲程重量:2
});
flightPath.setMap(map);
//使用飞行计划初始化google地图的函数
//有效地包含以下内容的航路点数组:
//纬度、经度和方位。下面是飞行计划
//是json对象的数组。
var updateMap=函数(飞行计划){
//航路点数据始终是空值列表
控制台日志(飞行计划);
//如何创建类似于的数组
//var FlightPlan坐标在适当的上方
//用于初始化谷歌地图
var latLngArray;
//如何转换为合适的lat/lng阵列
//waypointData.forEach(函数(下一步)){
//latLngArray.push(新的google.maps.LatLng)(
//next.lat,next.lng);
//}
//航路点数据始终是空值列表
控制台日志(latLngArray);
}
//加载网页后调用的脚本
window.onload=函数(){
新的QWebChannel(qt.webChannelTransport,
功能(通道){
console.log(channel.objects);
//每当路由数据更改时,调用updateMap插槽
var dataSource=channel.objects.routeIPC;
dataSource.routeChanged.connect(updateMap);
}
);
}
}
基本用法,例如仅使用新属性“ID”修改对象,请随意删除:
const normalArray = jsonArray.map(item => {
const constructedItem = {...item, id: generateRandomID()};
return constructedItem;
});
讽刺的是,你可以使用.map()方法。你能举个例子吗?抱歉,我是一个javascript新手。latLngArray.map()另外,您可能在其他地方定义了
waypointData
(更新映射中注释掉的代码),但是从您的描述来看,flightPlan
似乎是带坐标的数组,对吗?哦,这是一个对象数组。它不是JSON。