从JSON呈现多个Google Maps API V3标记 [英] Rendering multiple Google Maps API V3 markers from JSON

查看:102
本文介绍了从JSON呈现多个Google Maps API V3标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Google地图API V3通过PHP从DB输出的JSON绘制多个标记。



地图在页面上初始化,但不会使用标记填充。浏览器警告资源解释为其他,但是使用未定义的MIME类型传输。?



请进一步排除故障/调试的建议。

 <! - 将数据库中的点加载到位置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);
}
}
});

函数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
});
};


});

已验证的来自map-service.php的action示例JSON输出action = listpoints

  {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 : -  125.489998},{name:Argus Mountain,lat:49.538612,lng: -  125.389999},{name:Big Baldy Mountain,lat 49.759998,lng: -  126.129997}]} 


解决方案

我的问题源于我如何初始化原始地图对象。在我分享的代码中看不到...歉意。



我这样做了:

<$ p $ (map.canvas),myOptions);}

并应该做到这一点:

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

我在这篇文章中找到了解决方案在创建地图后添加标记 Thanks @yitwail!


Attempting to plot multiple markers using Google Maps API V3 from JSON output by PHP from DB.

Map is initialized on page, however it does not populate with markers. Browser warns "Resource interpreted as Other but transferred with MIME type undefined."?

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

{"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 !

这篇关于从JSON呈现多个Google Maps API V3标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆