【问题标题】:Markers won't show on Google Map, markers coordinates from jquery $.get call [closed]标记不会显示在谷歌地图上,标记坐标来自 jquery $.get 调用 [关闭]
【发布时间】:2014-08-23 07:47:56
【问题描述】:

我正在从 mysql 数据库中获取数据,并通过通过 $.get 访问的 laravel/php 将其 JSON 化,使用 jquery,它返回如下:

[{"id":45,"name":"Aberdeen","latitude":"-2.09410800","longitude":"57.15502200"},
{"id":46,"name":"Bangor","latitude":"-4.18029020","longitude":"53.20660020"},
{"id":47,"name":"Bath","latitude":"-2.36205010","longitude":"51.38600160"}]
//list goes on

然后我想在我的地图上添加标记,当我在 js 文件中手动定义上述数组并且未使用 $.get 时,单击侦听器起作用。使用$.get,我的代码如下所示:

$(document).ready(function () {

    function initialize(clubs) {
        var mapOptions = {
            center: new google.maps.LatLng(54.206845, -2.422000),
            zoom: 6
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

        var infowindow = new google.maps.InfoWindow(),
            marker, i;

        for (i = 0; i < clubs.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(clubs[i]['latitude'], clubs[i]['longitude']),
                map: map
            });

            google.maps.event.addListener(marker, 'click', (function (marker, i) {
                return function () {
                    infowindow.setContent(clubs[i]['name']);
                    infowindow.open(map, marker);
                    console.log('clicked on marker');
                    $('#club-info').html('club: ' + clubs[i]['name']);
                }
            })(marker, i));
        } // end for
    } // end function initialize

    $.get(
        'api/clubdata',

    function (data) {
        google.maps.event.addDomListener(window, 'load', initialize(data));
    });
});

HTML 是这样的(使用 bootstrap 作为样式):

<div class="row">
    <div class="col-md-6" id="map-canvas"></div>
    <div class="col-md-6" id="club-info"></div>
</div>

我似乎无法弄清楚为什么标记不显示,非常感谢任何帮助!

编辑: 我只是补充一点,数据很好,我已经通过 console.log 进行了检查。我觉得这是我编程方式的问题。

【问题讨论】:

  • 如果您在for 循环中记录clubs[i]['latitude'] 会怎样?
  • 另外marker = ... 应该是var marker = ...
  • 当我粘贴它时,我实际上从代码中的 for 循环开头删除了该行 - 哈哈。是的,它显示了所有 lats 的列表
  • 我建议你试试。
  • 好吧,我想通了,我搞砸了 - 很抱歉我浪费了你的时间

标签: javascript jquery ajax google-maps-api-3


【解决方案1】:

您不能使用指向 addDomListener 调用的函数指针传递参数:

google.maps.event.addDomListener(window, 'load', initialize(data));

改为这样做:

google.maps.event.addDomListener(window, 'load', function() {initialize(data)});

但是...您确实应该在回调 $.get 调用时这样做(当数据可用时)。

$.get(
    'api/clubdata',
    function(data){
        initialize(data);
    }
);

您的 JSON 中的经度和纬度属性也有误。应该是:

var data = [{
    "id": 45,
        "name": "Aberdeen",
        "longitude": "-2.09410800",
        "latitude": "57.15502200"
}, {
    "id": 46,
        "name": "Bangor",
        "longitude": "-4.18029020",
        "latitude": "53.20660020"
}, {
    "id": 47,
        "name": "Bath",
        "longitude": "-2.36205010",
        "latitude": "51.38600160"
}];

fiddle (without the JSON get)

【讨论】:

  • 谢谢,我会试一试 - 仅供参考:它确实按照我写的方式工作。
  • 它可能适用于少量数据,但如果 onload 事件在数据返回之前触发,则初始化函数将永远不会运行。它“工作”的唯一原因是因为您使用的语法不等待 onload 事件,它运行初始化函数并将其返回值(null)分配为回调函数。
【解决方案2】:

有一个愚蠢的时刻 - 经度和纬度混淆了。应该是这样写的:

var marker = new google.maps.Marker({
                position: new google.maps.LatLng(clubs[i]['longitude'], clubs[i]['latitude']),
                map: map
            });

【讨论】:

  • 如果是这种情况,您应该重命名您的经度和纬度属性以使其正确。 google.maps.LatLng 构造函数按纬度、经度的顺序获取其参数。
  • 哦,好吧 - 难怪我一开始就搞不定 - 非常感谢
  • @geocodezip 将此作为答案,我将其标记为正确
猜你喜欢
  • 2018-05-14
  • 2020-05-29
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
相关资源
最近更新 更多