简体   繁体   English

从JSON渲染多个Google Maps API V3标记

[英]Rendering multiple Google Maps API V3 markers from JSON

Attempting to plot multiple markers using Google Maps API V3 from JSON output by PHP from DB. 尝试使用来自DB的PHP从JSON输出的Google Maps API V3绘制多个标记。

Map is initialized on page, however it does not populate with markers. 地图在页面上初始化,但不会填充标记。 Browser warns "Resource interpreted as Other but transferred with MIME type undefined."? 浏览器警告“资源解释为其他,但转移MIME类型未定义。”?

Suggestions for further troubleshooting / debugging please. 请进一步排除故障/调试的建议。

        <!--  load points from database into Locations JSON -->
    $(document).ready(function () {
        $.getJSON("map-service.php?action=listpoints", function(json) {

        if (json.Locations.length > 0) {
            for (i=0; i<json.Locations.length; i++) {
                var location = json.Locations[i];
                    addMarker(location); 
                    }
                }
            });

        function addMarker(location) {
            var point = new google.maps.LatLng(location.lat, location.lng); 
            var marker = new google.maps.Marker({
                position:point,
                map: map,
                title: location.name
                }); 
            };


    });

Validated sample JSON output from map-service.php?action=listpoints 来自map-service.php?action = listpoints的验证样本JSON输出

{"Locations":[{"name":"Abco Mountain","lat":"49.424999","lng":"-125.855003"},{"name":"Adder Peak","lat":"49.248333","lng":"-125.320000"},{"name":"Alexandra Peak","lat":"49.738110","lng":"-125.489998"},{"name":"Argus Mountain","lat":"49.538612","lng":"-125.389999"},{"name":"Big Baldy Mountain","lat":"49.759998","lng":"-126.129997"}]}

My problem stemmed from how I had initialized the original map object. 我的问题源于我如何初始化原始地图对象。 It wasn't visible in the code I shared ... apologies. 它在我分享的代码中不可见......道歉。

I did this: 我这样做了:

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

and should have done this: 并且应该这样做:

this.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

I found the solution in this post Adding markers after map created Thanks @yitwail ! 我发现这个帖子中的解决方案在地图创建添加标记谢谢@yitwail!

Try using jquery $.each for your looping. 尝试使用jquery $ .each进行循环。

if (json && json.Locations) { if(json && json.Locations){

$.each(json.Locations, function() { addMarker(this); }); $ .each(json.Locations,function(){addMarker(this);});

}); });

Also, you'll probably want to call parseFloat on the lat and longiture inside your addMarker function. 此外,您可能希望在addMarker函数内的lat和longiture上调用parseFloat。

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

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