简体   繁体   English

Google Map标记未与Ajax JSON数据一起显示

[英]Google map markers are not displaying with ajax json data

I am getting latitude & longitude from database table and trying to display markers on ajax success . 我正在从数据库表中获取latitudelongitude ,并尝试在ajax success显示标记。 I am getting latitude & longitude on json format but when tried with loop, markers are not displaying. 我得到latitudelongitude上JSON格式,但是当与环试过,标志不显示。

My JSON Data: 我的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 map with Ajax Code: 带有Ajax代码的Google地图:

<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>

When I tried to pass static latitude & longitude inside the loop marker is displayed: 当我尝试在循环标记内传递静态latitudelongitude ,将显示:

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

but not working with dynamic with loop. 但不能与动态with循环一起使用。

Any idea why markers are not displaying? 知道为什么标记不显示吗?

Thanks. 谢谢。

You're passing the coordinates incorrectly to the google.maps.LatLng constructor. 您将坐标错误地传递给google.maps.LatLng构造函数。 There should be two parameters separated by a comma but you're concatenating them as a string. 应该有两个参数,以逗号分隔,但是您将它们串联为一个字符串。

The code should look like this: 该代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM