使用javascript/jquery从Google Maps API的动态列表填充数组

使用javascript/jquery从Google Maps API的动态列表填充数组,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,我有一个搜索应用程序,可以得到一个地方列表。这些位置和名称将位于属性标记中,例如 <div data-location="22.4245,-15.000" data-id="place_name_1">Place Name 1</div> <div data-location="23.4435,-13.000" data-id="place_name_2">Place Name 2</div> <div data-location="27.4

我有一个搜索应用程序,可以得到一个地方列表。这些位置和名称将位于属性标记中,例如

<div data-location="22.4245,-15.000" data-id="place_name_1">Place Name 1</div>
<div data-location="23.4435,-13.000" data-id="place_name_2">Place Name 2</div>
<div data-location="27.42755,-13.000" data-id="place_name_3">Place Name 3</div>
我需要做什么才能在javascript中将数据从html获取到数组


谢谢

使用jQuery,您可以解析HTML:

var beaches = [];    
$('div[data-location][data-id]').each(function() {
    beaches.push([
        [$(this).attr('data-id')]
            .concat(
                $(this).attr('data-location').split(","),
                [2]
            )
    ]);
});
var beaches = [];

// iterate over all <div>s (assuming they're always in that format)
$('div').each(function (index, div) {

    // break apart the comma-separated coordinates in the data-location attribute
    var coords = $(div).data('location').split(',');

    // add this location to the beaches array (with z-index 0)
    beaches.push([$(div).text(), coords[0], coords[1], 0]);

});
var=[];
//迭代所有的(假设它们始终采用该格式)
$('div')。每个函数(索引,div){
//在“数据位置”属性中拆分逗号分隔的坐标
var coords=$(div).data('location').split(',');
//将此位置添加到Beats数组(z索引为0)
push([$(div.text(),coords[0],coords[1],0]);
});
一些应用程序可能将坐标编码为经度、纬度。如果是这种情况,则需要交换
coords
的位置,即:
beats.push([$(div.text(),coords[1],coords[0],0])

请记住,数组非常特定于您所指的示例(实际上是
setMarkers()
函数)。

尝试以下操作:

var $beaches = [];

$.find("div").each(function(){

     var $loc = [];

    $loc.push($(this).html());
    $loc.push($(this).attr("data-location"));
    $loc.push($(this).attr("data-id"));
    $loc.push(2);

    $beaches.push($loc);
 });

它给了我.each()函数的问题。它似乎比它应该填充的数组多得多?是的,
$(这个)
也可以工作。您可以省略
div
参数。
var $beaches = [];

$.find("div").each(function(){

     var $loc = [];

    $loc.push($(this).html());
    $loc.push($(this).attr("data-location"));
    $loc.push($(this).attr("data-id"));
    $loc.push(2);

    $beaches.push($loc);
 });