我试图创建一个显示数据库中所有位置的地图。 我可以为一个对象做到这一点,但我不能为所有。如何从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));
我不知道我是如何结合这些代码并从数据库位置获取具有多个标记的地图。感谢您的帮助
我做的那个位。{{== == coukd刚刚被Shop.get params.shopId。在Java脚本世界里,你也可以调试。通常情况下,你的对象返回为变成scope.shop的json将使用对象格式。因此,如果您在该段中输入console.log('json object is'+ JSON.stringify(json)),然后打开您的浏览器开发控制台,它应该记录您的整个输出 – Vahid
谢谢@vahid。这解决了我的问题。 –