将纬度/经度坐标推入Google地图数组 [英] Push lat/long coordinates to an array for Google maps
本文介绍了将纬度/经度坐标推入Google地图数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据一组div的数据属性为Google地图生成地图标记.我需要数组最终看起来像这样:
I am trying to generate map markers for a Google map based off data attributes of a set of divs. I need the array to end up looking like this:
var markers = [
[51.503454,-0.119562],
[51.499633,-0.124755]
];
这是我到目前为止尝试过的:
Here's what I have tried so far:
var markers = [];
$(function() {
$('.location').each(function() {
var lat = $(this).attr('data-lat'),
lng = $(this).attr('data-lng');
markers.push(lat,lng);
});
console.log(markers);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="location" data-lat="51.503454" data-lng="-0.119562"></div>
<div class="location" data-lat="51.499633" data-lng="-0.124755"></div>
如何成对获取纬度/经度坐标而不是4个单独的值?
How do I get the lat/lng coordinates in pairs instead of 4 separate values?
推荐答案
您可以使用map
方法,并为每个元素返回具有lat
,lng
值的数组.
You could use map
method and return array with lat
, lng
values for each element.
const markers = $('.location').map(function() {
return [[$(this).data('lat'), $(this).data('lng')]]
}).get();
console.log(markers)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="location" data-lat="51.503454" data-lng="-0.119562"></div>
<div class="location" data-lat="51.499633" data-lng="-0.124755"></div>
这篇关于将纬度/经度坐标推入Google地图数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文