2016-08-19 122 views
0

我試圖創建一個顯示數據庫中所有位置的地圖。 我可以爲一個對象做到這一點,但我不能爲所有。如何從Grails中的JSON對象獲取數據庫變量

在我的控制,我得到的所有商店和發送到angularjs文件

def getalllocation(){ 
    def shops=Shop.findAll() 
    [shops:shops] as JSON 
} 

javascriptfile; //這適用於一家商店

$scope.getshopinfo = function() { 
     $http.post("/shop/getshoplocation", data = {shopId: $("#shopId").val()}).success(function (json) { 
      $scope.shop = json; 
      //$scope.map = {center: {latitude: 45, longitude: -73}, zoom: 8}; 
      //$scope.map = {center: {latitude: $scope.shop.shopLattitude, longitude: $scope.shop.shopLongitude}, zoom: 8}; 
      $scope.map = { 
       center: {latitude: $scope.shop.shopLattitude, longitude: $scope.shop.shopLongitude}, 
       zoom: 10, 
       events: mapevents 
      }; 
      $scope.searchbox = { 
       template: "searchbox.tpl.html", 
       events: searchevents 
      }; 
      $scope.marker = { 
       coords: {latitude: $scope.shop.shopLattitude, longitude: $scope.shop.shopLongitude}, 
       id: '2' 
      }; 
      $("#shopLattitude").val($scope.shop.shopLattitude); 
      $("#shopLongitude").val($scope.shop.shopLongitude); 
      // $scope.$apply(); 
     }).finally(function() { 
     }); 
    }; 
    if ($("#shopId").val()) { 
     $scope.getshopinfo(); 
    } 

}]); 

我可以用多個標記創建地圖,但不能從數據庫創建地圖;

var locations = [ 
[ 
    "New Mermaid", 
    36.9079, 
    -76.199, 
    1, 
    "Georgia Mason", 
    "", 
    "Norfolk Botanical Gardens, 6700 Azalea Garden Rd.", 
    "coming soon" 
], 
[ 
    "1950 Fish Dish", 
    36.87224, 
    -76.29518, 
    2, 
    "Terry Cox-Joseph", 
    "Rowena's", 
    "758 W. 22nd Street in front of Rowena's", 
    "found" 
]] 




var marker, i; 
for (i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
    map: map 
}); 

google.maps.event.addListener(marker, 'click', (function (marker, i) { 
    return function() { 
     infowindow.setContent(locations[i][0], locations[i][6]); 
     infowindow.open(map, marker); 
    } 
})(marker, i)); 

我不知道我是如何結合這些代碼並從數據庫位置獲取具有多個標記的地圖。感謝您的幫助

回答

0

我想我理解這裏的問題。由於你的否定,我也提出了你的問題。我想這是因爲你的問題實際上包含了很多代碼,並沒有涉及到實際問題。

無論如何,我希望通過我即將說的話來幫助你和其他人以不同的方式處理這種問題。

該位樣的矛盾:

創建地圖,顯示在數據庫中的所有位置。我能做到這一點的一個對象,但我不能爲所有,在控制器代碼看時:

def getalllocation(){ 
    def shops=Shop.findAll() 
    [shops:shops] as JSON 
} 

在你的GSP影響你

$ http.post(「/店/ getshoplocation」,數據= {shopId: $( 「#shopId」)VAL()})成功(函數(JSON){

您發送PARAMS但隨後在控制你。查找所有對象

def getalllocation(){ 
     // Firstly when creating new methods always use debug 
     // like this to figure out what you are getting 
     println "my params are $params" 

     //I can see you are passing in an actual shopId so 
     //lets add some logic to either list all or a given shopId 
     //No idea what the object here is 
     def shops 
     if (params.shopId) { 
       shops=Shop.where{id==params.shopId}.findAll() 
     } else { 
      shops=Shop.findAll() 
     } 
     [shops:shops] as JSON 
    } 

有了這個,你應該希望它能夠工作,因爲在你的代碼中,當點擊一個商店ID時它會顯示狀態。

至於你的矛盾,你說你想讓它顯示所有的商店,並只顯示一個

「雖然你的代碼已被定義爲通過shopId到控制器?」

無論是它無法顯示或顯示當前地圖的原因是因爲您返回的是所有商店條目的JSON地圖,但是當您收到json時,您似乎試圖解析包含json的列表列出一次。我認爲你需要遍歷它。

Something like this

$http.post("/shop/getshoplocation", data = {shopId: $("#shopId").val()}).success(function (json) { 

     $scope.companies = json.forEach(function(entry) { 
      center: {latitude: $entry.shopLattitude, longitude: $entry.shopLongitude}, 
       zoom: 10, 
       events: mapevents 
     } 
} 

我沒有訪問任何這一點,這樣的事情畢竟上面的是所有的猜測和可能不是100%它只是給你一個關於它可能出錯的想法。

+0

我做的那個位。{{== == coukd剛剛被Shop.get params.shopId。在Java腳本世界裏,你也可以調試。通常情況下,你的對象返回爲變成scope.shop的json將使用對象格式。因此,如果您在該段中輸入console.log('json object is'+ JSON.stringify(json)),然後打開您的瀏覽器開發控制檯,它應該記錄您的整個輸出 – Vahid

+0

謝謝@vahid。這解決了我的問題。 –