2015-11-25 35 views
0

我不想使用任何包。 我想使用Ajax請求從數據庫中獲取GPS位置到進入谷歌地圖,但有幾個問題 1.這是我的樹枝地圖腳本使用控制器在symfony2中從數據庫獲取gps位置

<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> 
  • 這是從服務器端獲取GPS位置控制器

    公共功能的indexAction(請求$請求) {

    $ EM = $這 - > 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));

  • }

  • 最後這是錯誤我得到,而不是顯示在地圖上

    { 「位置」:[{ 「LAT」: 「13.459326003707657」, 「茇」: 「 - 16.580506139062436」, 「managerName」: 「拉明大號詹納」, 「地址」: 「班珠爾」, 「接觸」: 「3930050」}]}

  • +0

    的getResult()返回一個數組,我認爲你需要要麼使用getSingleResult()或返回第一個數組元素在你的JSON響應中。 –

    +0

    我不認爲這是問題,即使當我使用這個 返回新的JsonResponse(array('location'=> $ entities [0])); 是一回事 –

    回答

    0

    「的數據「是對你的ajax請求的迴應。它包含在位置的「位置」一個對象,所以你應該改變

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

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

    感謝您耐心 你的意思 新google.maps.LatLng(data.location.lat,data.location.lat) 代替 新google.maps.LatLng(data.lat,數據.lat) –

    +0

    是的,你試過了嗎? –

    +0

    我沒有,但仍然是相同的 我客我實現了控制器 可以請你看看 $ resultsee = $ EM->的createQuery('選擇a.lat,a.longi,a.managerName的方式, a.address,來自AdminBundle的a.contacts:location a'); $ entities = $ resultsee-> getResult(); 返回新的JsonResponse($ entities [0]); –