Google 地图标记未显示 ajax json 数据

Google map markers are not displaying with ajax json data

我正在从数据库 table 中获取 latitude & longitude 并尝试在 ajax success 上显示标记。我在 json 格式上得到 latitude & longitude 但是当尝试循环时,标记没有显示。

我的JSON数据:

[
    {"latitude":"23.046100780353495","longitude":"72.56860542227514"},
    {"latitude":"23.088427701737665","longitude":"72.49273109366186"},
    {"latitude":"23.061264193197644","longitude":"72.68224525381811"},
    {"latitude":"22.977212139977677","longitude":"72.52191352774389"},
    {"latitude":"23.002180435752084","longitude":"72.47590827872045"},
    {"latitude":"23.108638843843046","longitude":"72.49444770743139"}
]

Google 映射 Ajax 代码:

<script type="text/javascript">
// Check DOM Ready
$(document).ready(function() {

    // Execute
    (function() {
        // Map options
        var options = {
            zoom: 6,
            center: new google.maps.LatLng(23.039567700000000000, 72.566004499999960000), // Centered
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            mapTypeControl: false
        };

        // Init map
        var map = new google.maps.Map(document.getElementById('map_canvas'), options);

        $.ajax({
            url: 'get-locations.php',
            success:function(data){
                var obj = JSON.parse(data);
                var totalLocations = obj.length;

                for (var i = 0; i < totalLocations; i++) {

                    // Init markers
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(obj[i].latitude + ',' + obj[i].longitude),
                        map: map,
                        title: 'Click Me ' + i
                    });

                    // Process multiple info windows
                    (function(marker, i) {
                        // add click event
                        google.maps.event.addListener(marker, 'click', function() {
                            infowindow = new google.maps.InfoWindow({
                                content: 'Hello, World!!'
                            });
                            infowindow.open(map, marker);
                        });
                    })(marker, i);

                }

            }
        });
    })();
});
</script>

当我尝试在循环标记内传递静态 latitude & longitude 时显示:

position: new google.maps.LatLng(23.046100780353495,72.56860542227514),

但不能使用带有循环的动态。

知道为什么标记没有显示吗?

谢谢。

您将坐标错误地传递给 google.maps.LatLng 构造函数。应该有两个用逗号分隔的参数,但您将它们连接成一个字符串。

代码应如下所示:

position: new google.maps.LatLng(obj[i].latitude, obj[i].longitude)