如何从Grails中的JSON对象获取数据库变量
问题描述:
我试图创建一个显示数据库中所有位置的地图。 我可以为一个对象做到这一点,但我不能为所有。如何从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));
我不知道我是如何结合这些代码并从数据库位置获取具有多个标记的地图。感谢您的帮助
答
我想我理解这里的问题。由于你的否定,我也提出了你的问题。我想这是因为你的问题实际上包含了很多代码,并没有涉及到实际问题。
无论如何,我希望通过我即将说的话来帮助你和其他人以不同的方式处理这种问题。
该位样的矛盾:
创建地图,显示在数据库中的所有位置。我能做到这一点的一个对象,但我不能为所有,在控制器代码看时:
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的列表列出一次。我认为你需要遍历它。
$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%它只是给你一个关于它可能出错的想法。
我做的那个位。{{== == coukd刚刚被Shop.get params.shopId。在Java脚本世界里,你也可以调试。通常情况下,你的对象返回为变成scope.shop的json将使用对象格式。因此,如果您在该段中输入console.log('json object is'+ JSON.stringify(json)),然后打开您的浏览器开发控制台,它应该记录您的整个输出 – Vahid
谢谢@vahid。这解决了我的问题。 –