Javascript 如何根据传感器编号显示位置数组中的一个位置
我有一系列像这样的纬度和经度 我可以在谷歌地图上显示所有这些位置,但如何根据传感器编号显示一个位置Javascript 如何根据传感器编号显示位置数组中的一个位置,javascript,google-maps,Javascript,Google Maps,我有一系列像这样的纬度和经度 我可以在谷歌地图上显示所有这些位置,但如何根据传感器编号显示一个位置 var locations = [ ['Sensor 1', -33.890542, 151.274856, 4], ['Sensor 2', -33.923036, 151.259052, 5], ['Sensor 3', -34.028249, 151.157507, 3], ['Sensor 4', -33.80010128657071, 15
var locations = [
['Sensor 1', -33.890542, 151.274856, 4],
['Sensor 2', -33.923036, 151.259052, 5],
['Sensor 3', -34.028249, 151.157507, 3],
['Sensor 4', -33.80010128657071, 151.28747820854187, 2],
['Sensor 5', -33.950198, 151.259302, 1]
];
我看不到你的代码,所以很难知道 这是一个有效的解决方案,单击html元素时调用initialize 这将使用新标记重置地图
- 使用参数“select”定义了initialize()
- 在map变量下面(但在initialize内部)做了一个标记,它使用先前定义的“select”参数拾取数据
- 使用html元素调用带有参数值的initialize
<!DOCTYPE html>
<html>
<head>
<style>
#map-canvas {
width: 500px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
var locations = [
['Sensor 1', -33.890542, 151.274856, 4],
['Sensor 2', -33.923036, 151.259052, 5],
['Sensor 3', -34.028249, 151.157507, 3],
['Sensor 4', -33.80010128657071, 151.28747820854187, 2],
['Sensor 5', -33.950198, 151.259302, 1]
];
function initialize(select) {
var myLatlng = new google.maps.LatLng(-33.890542, 151.274856);
var mapOptions = {
zoom: 9,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[select][1], locations[select][2]),
map: map
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<p onclick="initialize(0)">Sensor 1</p>
<p onclick="initialize(1)">Sensor 2</p>
<p onclick="initialize(2)">Sensor 3</p>
<p onclick="initialize(3)">Sensor 4</p>
<p onclick="initialize(4)">Sensor 5</p>
<div id="map-canvas"></div>
</body>
</html>
#地图画布{
宽度:500px;
高度:400px;
}
变量位置=[
[‘传感器1’,-33.890542151.274856,4],
[‘传感器2’,-33.923036151.259052,5],
[‘传感器3’,-34.028249151.157507,3],
[‘传感器4’,-33.80010128657071151.28747820854187,2],
[‘传感器5’,-33.950198151.259302,1]
];
函数初始化(选择){
var mylatng=new google.maps.LatLng(-33.890542151.274856);
变量映射选项={
缩放:9,
中心:myLatlng
}
var map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
var marker=new google.maps.marker({
位置:新建google.maps.LatLng(位置[选择][1],位置[选择][2]),
地图:地图
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
传感器1
传感器2
传感器3
传感器4
传感器5