在Symfony Twig中找不到变量

I am new to the Symfony framework. What I am trying to do is to fetch the data from database and display them in Google Map. Here is my controller class:

//removed code

However, when I tried to run it, it says 'Variable "locations" does not exist.'. I followed this Tutorial. I not sure why the locations is not declared since I already did it in Controller class.

Sorry but I am new to Symfony. Any helps will be appreciated! Thanks!

If you want to access variable in script use it like this.

<script>
{% for location in locations %}
    var map;
    function initMap() {
        var myLatLng = {lat: '{{ location.latitude }}', lng: '{{ location.latitude }}' };
        map = new google.maps.Map(document.getElementById('map'), {
            center: myLatLng,
            zoom: 10
        });

        var marker = new google.maps.Marker({
           position: myLatLng,
           map: map,
           title: 'Hello World!'
       });
   }
{% endfor %}
</script>

No need to repeat script tag in loop.