javascriptgoogle-mapsgoogle-maps-api-3slimuserfrosting

Google maps Type Error: a is null


I have looked at similar questions and can't seem to find a solution. I am using UserFrosting which is based on TWIG/Slim framework. My code is below, and produces the following error with no map displaying.

TypeError: a is 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>

Solution

  • You should not be expecting a response directly from MarkersController.php if you are using UserFrosting/Slim properly.

    Supposing your MarkersController.php has a method like getMarkersJSON() that generates the JSON data you need, you need to create a route in your index.php like this:

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

    Then in your jQuery call to getJSON, you would do something like:

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

    You can test that your route is working properly by simply navigating to http://example.com/markers/json and ensuring that you see the raw data you expect.

    I would advise you to take a look at the UserFrosting documentation to make sure you understand the front controller pattern.