谷歌地图标记不显示ajax json数据

Google map markers are not displaying with ajax json data

本文关键字:ajax json 数据 显示 地图 图标 谷歌      更新时间:2024-03-10

我得到latitude&CCD_ 2并尝试在CCD_ 3上显示标记。我得到了latitude&json格式的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"}
]

带有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)