Google地图类型错误:a为空

时间:2015-08-21 15:59:35

标签: javascript google-maps google-maps-api-3 slim userfrosting

我看过类似的问题,似乎无法找到解决方案。我正在使用基于TWIG / Slim框架的UserFrosting。我的代码在下面,并且在没有地图显示的情况下产生以下错误。

TypeError:a为null1 main.js:19:628

<!DOCTYPE html>
<html lang="en">
    {% include 'components/head.html' %}

    <body>
        <div id = "wrapper">
            {% include 'components/nav-account.html' %}
            <div id = "page-wrapper">
                {% include 'components/alerts.html' %}

                <div class = "container">
                    <style type = "text/css">
                        html, body, #map-canvas{ height:100%;
                                                 margin:0;
                                                 padding:0;
                        }
                    </style>

                    <script type = "text/javascript"
                            src = "https://maps.googleapis.com/maps/api/js?
                            key=AIzaSyB76xBqfQdgOLV77VK3JZ09vWwk8brkMFs">
                    </script>
                    <script type="text/javascript">
                        var map = null;
                        var iLoadPoints = 0;
                        function addMarker(lat, lng) {
                            marker = new google.maps.Marker({
                                position: new google.maps.LatLng(lat, lng),
                                map: map,
                            });
                        }


                        function initialize() {
                            var mapOptions = {
                                center: {lat: 54.872128, lng: -6.284874},
                                zoom: 13
                            };
                            map = new google.maps.Map(document.getElementById('map-canvas'),
                                    mapOptions);


                            $(document).ready(function () {

                                $.getJSON('MarkersController.php', function (data) {


                                    var locations = JSON.parse(data);

                                    for (var i = 0; i < locations.length; i++) {
                                        addMarker(locations[i].lat, locations[i].lng);
                                    }
                                });
                            });


                        }


                        google.maps.event.addListenerOnce(map, 'idle', function () {
                            iLoadPoints += 1;
                            if (iLoadPoints === 2) {
                                initialize();
                            }
                        });

                        google.maps.event.addDomListener(window, 'load', function () {
                            iLoadPoints += 1;
                            if (iLoadPoints === 2) {
                                initialize();
                            }
                        });



                    </script>

                    <div id="map-canvas" style="height:600px; width:600px;
                         margin-top:100px; margin-bottom: 100px;
                         ">

                    </div>
                </div>
                {% include 'components/footer.html' %}    

            </div>
        </div>
    </body>

</html>

2 个答案:

答案 0 :(得分:1)

如果正确使用UserFrosting / Slim,则不应期望直接来自MarkersController.php的响应。

假设您的MarkersController.php有类似getMarkersJSON()的方法生成所需的JSON数据,您需要在index.php中创建路径,如下所示:

$app->get('/markers/json/?', function () use ($app) {
    $controller = new UF\MarkersController($app);
    return $controller->getMarkersJSON();
}); 

然后在你对getJSON的jQuery调用中,你会做类似的事情:

$.getJSON('/markers/json', function (data) {
    ...
});

只需导航到http://example.com/markers/json并确保您看到所需的原始数据,即可测试您的路线是否正常运行。

我建议你看一下UserFrosting documentation以确保你理解前控制器模式。

答案 1 :(得分:0)

你应该在src属性中传递你的函数,如下所示:

src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyB76xBqfQdgOLV77VK3JZ09vWwk8brkMFs&callback=initialize"

这是一个有效的example