【问题标题】:How to take screenshot of googlemap from web page using PhantomJs?如何使用 PhantomJs 从网页截取 googlemap 的屏幕截图?
【发布时间】:2017-02-23 08:11:45
【问题描述】:

我的要求是截取网页谷歌地图的屏幕截图。我在谷歌地图上动态绘制标记和折线。我正在使用 PhantomJS 进行截图。但屏幕截图没有捕获正在显示的完整谷歌地图在网页上。

Html and javascript for loading and display google map
----------
     <script>
            function initMap() {
                    var mapOptions = {
                        zoom: 15,
                        center: new google.maps.LatLng(28.5354383, 77.26393259999998)
                    };
                    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
                    var project_id = $('#project').data('url')
                    var latt = substation[0]
                    var lngg = substation[1]
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(latt, lngg),
                        title: new google.maps.LatLng(latt, lngg).toUrlValue(6),
                        map: map,
                        draggable: true,
                        label: {
                            color: 'black',
                            fontWeight: 'bold',
                            text: "Sub-Station",
                        },
                        icon: {
                            labelOrigin: new google.maps.Point(4, 25),
                            url: '/assets/sub_station.png',
                        }
                    });
                    google.maps.event.addListener(marker, "dragend", function(event) {
                        $.getScript("/update_pole.js?project_id=" + project_id + "&Type=SS" + "&new_cordinated=" + event.latLng.toString().slice(1, -1), function(data) {});
                    });
                    var lattt = feeder[0]
                    var lnggg = feeder[1]
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lattt, lnggg),
                        title: new google.maps.LatLng(lattt, lnggg).toUrlValue(6),
                        map: map,
                        draggable: true,
                        label: {
                            color: 'black',
                            fontWeight: 'bold',
                            text: "Feeder",
                        },
                        icon: {
                            labelOrigin: new google.maps.Point(4, 25),
                            url: '/assets/ptw.png',
                        }
                    });

用于截屏的 PhantomJs

var page = require('webpage').create();
  page.open('https://medhajtest.herokuapp.com/admin/visibilities/demo?id=586b524712598e0004f440d0', function() {
  page.viewportSize = { width: 1440, height: 900 };
  var clipRect = page.evaluate(function(){
    return document.querySelector("div#map").getBoundingClientRect();
  });
  page.render('github.png');
  phantom.exit();
});

预期的屏幕截图

当前即将截屏

【问题讨论】:

  • 看起来您期待的是带有缩小视图的谷歌地图,对吧?
  • 不,我想截取谷歌地图上标记和折线绘制在地图上的所有部分的屏幕截图。

标签: javascript ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 phantomjs


【解决方案1】:

注意屏幕截图的尺寸,400x300。这是默认的 PhantomJS 视口大小。您必须在打开页面之前设置所需的视口大小。

var page = require('webpage').create();

// Got to set this before opening a page
page.viewportSize = { width: 1440, height: 900 };

page.open('https://medhajtest.herokuapp.com/admin/visibilities/demo?id=586b524712598e0004f440d0', function() {
    var clipRect = page.evaluate(function(){
        return document.querySelector("div#map").getBoundingClientRect();
    });
    page.render('github.png');
    phantom.exit();
});

【讨论】:

  • 您好 Vaviloff 现在我面临另一个障碍,想​​在活动管理员中捕获页面的屏幕截图,但是当我尝试这样做时,它要求登录?是否可以在不登录的情况下进行屏幕截图?你能帮帮我吗?
  • 是否可以在不登录的情况下在普通浏览器中查看管理区域?不,当然不。您应该搜索如何使用 PhantomJS 登录网站。
猜你喜欢
  • 2010-10-16
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多