我正在尝试创建一个显示数据库中所有位置的 map 。
我可以对一个对象执行此操作,但不能对所有对象执行此操作。
在我的 Controller 中,我得到了所有商店并发送到angularjs文件
def getalllocation(){
def shops=Shop.findAll()
[shops:shops] as JSON
}
javascript文件; //这适用于一家商店
$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();
}
}]);
我可以用多个标记创建一个 map ,但是不能从数据库中创建。
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));
我不知道如何结合这些代码并从数据库位置获取带有多个标记的 map 。感谢帮助
最佳答案
我想我明白这里的问题。自从您提出否定意见以来,我也提高了您的问题。我想这是由于您的问题实际上包含很多代码而没有实际问题那么详细。
无论如何,我希望通过我要说的内容,可以帮助您和其他人以不同的方式处理此类问题。
这有点矛盾:
创建一个显示数据库中所有位置的 map 。在查看 Controller 代码时,我可以对一个对象执行此操作,但不能对所有对象执行此操作:
def getalllocation(){
def shops=Shop.findAll()
[shops:shops] as JSON
}
实际上在您的gsp中
$http.post("/shop/getshoplocation", data = {shopId: $("#shopId").val()}).success(function (json) {
您正在发送一个参数,但随后在 Controller 中查找所有对象。
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时显示出来。
关于您的矛盾,您说您希望它显示所有商店,并且只显示一个
"although your code has been defined to pass shopId to controller ?"
不管怎样,它之所以无法显示或显示当前 map ,是因为您要返回所有商店条目的JSON map ,但是当您收到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%,它只是让您知道错误可能出在哪里。
关于javascript - 如何从Grails中的JSON对象获取数据库变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038885/