0

I am using Gridstack with Knockout.js. I am using http://troolee.github.io/gridstack.js/demo/knockout.html to create Gridstack widgets. I added a Google map in one of these widgets. The map appears with a scroll bar. Also, I need to have some kind of heading, as it is in its present state. The main problem is that the dimension of the map is fixed and when I resize the widget, the map does not fit in the widget automatically. Here is my full code:

ko.components.register('dashboard-grid', {
        viewModel: {
            createViewModel: function (controller, componentInfo) {
                var ViewModel = function (controller, componentInfo) {
                    var grid = null;

                    this.widgets = controller.widgets;
                    this.id = controller.id;

                    this.afterAddWidget = function (items) {
                        if (grid == null) {
                            grid = $(componentInfo.element).find('.grid-stack').gridstack({
                                auto: false,
                                animate: true
                            }).data('gridstack');
                        }

                        var item = _.find(items, function (i) { return i.nodeType == 1 });
                        grid.addWidget(item);
                        ko.utils.domNodeDisposal.addDisposeCallback(item, function () {
                            grid.removeWidget(item);
                        });
                    };
                };

                return new ViewModel(controller, componentInfo);
            }
        },
        template:
            [
                '<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">',
                '   <div class="grid-stack-item" data-bind="attr: {\'data-gs-x\': $data.x, \'data-gs-y\': $data.y, \'data-gs-width\': $data.width, \'data-gs-height\': $data.height, \'data-gs-auto-position\': $data.auto_position}">',
                '       <div class="grid-stack-item-content" >',
                '           <header style="background-color: green;"> ',
                '               <button data-bind="click: $root.deleteWidget">Delete me</button><br>',
                '               <label><strong data-bind="text: $root.ids"></strong></label>',
                '           </header>',
                '           <div data-bind="attr: {id: $root.ids}">',
                '           </div>',
                '       </div>',
                '   </div>',
                '</div> '
            ].join('')
    });

    $(function () {
        var ids;
        var Controller = function () {
            var self = this;

            this.widgets = ko.observableArray([]);
            this.id = ko.observable("google-map");
            this.ids = "";

            this.addNewWidget = function () {
                this.ids = this.id();   // this.ids is used to give id to div elements
                this.widgets.push({
                    x: 0,
                    y: 0,
                    width:4,
                    height:5,
                    auto_position: true,
                });
                // calling method to draw map
                createWidget(this.ids);
                return false;
            };

            this.deleteWidget = function (item) {
                self.widgets.remove(item);
                return false;
            };
        };

       ko.applyBindings(new Controller());

    });

    function createWidget(id) {

        var dataArray, latitude, longitude, speed;

        // Load the Visualization API and the corechart package.
        try {
            google.charts.load('current', {'packages':['map', 'gauge']});

            // Set a callback to run when the Google Visualization API is loaded.
            google.charts.setOnLoadCallback(drawChart);
        }
        catch ( err ) {
            //console.log(err);
        }

        function drawChart() {

            latitude = Number(12.93213);
            longitude = Number(77.695185);

            var mapOptions = {
                showTooltip: true,
                showInfoWindow: true,
                zoomLevel: 16, // range: 0-21 for "roadmap" map type
                mapType: 'roadmap',
                enableScrollWheel: true,
                draggable: false,
            };

            var coordinates = "Latitude : " + latitude + "\nLongitude : "+ longitude;
            var mapData = google.visualization.arrayToDataTable([
              ['Lat', 'Long', 'Name'],
              [latitude, longitude, coordinates]
            ] );  

            try {
                //var chart = new google.visualization.Gauge(document.getElementById('gauge'));
                var map = new google.visualization.Map(document.getElementById(id));
                map.draw(mapData, mapOptions);
            }

            catch (err) {
                console.log(err);
            }
        }   // drawChart() ends here
    }   // createWidgets() ends here
  • 1
    This is very similar to your original question. you are probably going to get alot of downvotes for these styles of questions, and then you will be unable to ask any more questions, and it is unlikely you are going to get much help unless you try to set up a minimal reproducible example of what you currently have. at the minimum post a small portion of the code you have tried. better yet make a fiddle or use the code snippet tool to create a runnable example. and explain what your expected results are. – Bryan Dellinger Mar 16 '17 at 13:53
  • @Bryan Dellinger I was able to render a google map in the widget. Now, the problem comes while resizing the widget. I have edited the question and also attached the code snippet. – manish choudhary Mar 17 '17 at 12:05

0 Answers0