nur yigit nur yigit - 1 year ago 118
Groovy Question

How to get database variables from JSON object in grails

I'm trying to create a map which show all location in database.
I can do this for one object but I could not for all.

In my controller I get all shops and send to angularjs file

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

javascriptfile; //this works for one shop

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


I can create a map with multiple markers but not from the database;

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

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]);, marker);
})(marker, i));

I dont know how I combine this codes and get a map with multiple markers from database location. thanks for help

Answer Source

I think I understand the issue here. I have also upped your question since you got a negative. I guess it is due to your question actually containing a lot of code and not so much detail as to actual issue.

Anyways I hope by what I am about to say will help you and others approach situations in a different way when it comes to problems like this.

This bit kind of contradicts:

create a map which show all location in database. I can do this for one object but I could not for all, when looking at the controller code:

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

In effect in your gsp you have

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

You are sending a params but then in the controller you are looking up all objects.

 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) {
        } else {
        [shops:shops] as JSON

With that you should hopefully having it working, since in your code it does state show it when a shop id is clicked.

As to your contradiction, you say you want it to show all shops and it only displays one

"although your code has been defined to pass shopId to controller ?"

Eitherway the reason it can't display or show the current map is because you are returning a JSON map of all your shop entries but then when you receive the json you appear to attempt to parse the list containing the json lists once. I think you need to iterate through it.

Something like this:

  $"/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

I don't have access to any of this, and all of the above after something like this is all guess work and may not be a 100% it is just giving you an idea as to where it may be going wrong.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download