0
votes

I don't want to use any bundle. I want to get gps location from the DB to the into the google map using Ajax request but got few issue 1. this is the map script in my twig

<script>

 function int()
 {
    var myLatlng = new google.maps.LatLng(49.47143, 11.107489999999984);
    var mapOptions = {
        zoom: 8,
        center: myLatlng
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    var marker;

    $.ajax({
        type: "GET",
        url: "{{ path('home_page') }}",
        data: "addr="+encodeURI("{{ location.address }}, {{ location.contacts }}, {{ location.managerName }}"),
        success: function(data) {
                marker = new google.maps.Marker({
                position: new google.maps.LatLng(data.lat, data.longi),
                map: map,
                title: 'test'
            });
        }
    });
 }   
google.maps.event.addDomListener(window, 'load', init);

</script>
  1. this is the controller that is getting the GPS location from the server end

    public function indexAction(Request $request) {

    $em = $this->getDoctrine()->getEntityManager();

    $resultsee=$em->createQuery('select a.lat,a.longi,a.managerName,a.address,a.contacts from AdminBundle:location a'); $entities = $resultsee->getResult();

    return new JsonResponse(array('location' => $entities));

}

  1. Finally this is the error I get instead of displaying on the map

    {"location":[{"lat":"13.459326003707657","longi":"-16.580506139062436","managerName":"Lamin L Janneh","address":"Banjul","contacts":"3930050"}]}

1
getResult() returns an array, I think you need to either use getSingleResult() or return the first array element in your JSON response.Spas Bobchev
I don't think that's the issue even when I use this return new JsonResponse(array('location' => $entities[0])); is the same thingLL Janneh

1 Answers

0
votes

"data" is the response to your ajax request. It contains one object at position "location" so you should change

new google.maps.LatLng(data.lat, data.longi)

to

new google.maps.LatLng(data.lat, data.longi)