Google Map screen capture is not working for marker and marker cluster using html2canvas

后端 未结 3 653
北恋
北恋 2021-01-15 17:26

I am doing python project using flask where I used google map api to show the map in the project. I implement html2canvas script to capture the map successfully. But I hav

相关标签:
3条回答
  • 2021-01-15 18:16

    1) Use useCORS:true or use proxy, never use both at the same time.

    2) Your routes are different, see:

    • virtual_path = '/gpsDataMap/images/'

    • @surveyApp_module.route('/gpsDataMap/html2canvas/images/<image>')

    3) Your proxy route, seems to be wrong (in your Javascript):

    • "proxy":"/surveyApp/gpsDataMap/html2canvas-proxy",

    • @surveyApp_module.route('/gpsDataMap/html2canvas-proxy')


    You do not realize all errors, why the userCORS with the "PROXY" get confused.

    Fix all routes (routes are their virtual paths) and fix your javascript (don't use userCORS:), see:

    $(window).load(function(){
        $('#saveMap').click(function(){
            html2canvas(document.getElementById('map'), {
                    "logging": true, //Enable log (use Web Console for get Errors and Warnings)
                    //useCORS:true, "COMMENTED", remove useCORS
                    "proxy": YOUR_FIXED_ROUTE,
                    "onrendered": function(canvas) {
                        var img = new Image();
                        img.onload = function() {
                            img.onload = null;
                            document.body.appendChild(img);
                        };
    
                        img.onerror = function() {
                            img.onerror = null;
                            if(window.console.log) {
                                window.console.log("Not loaded image from canvas.toDataURL");
                            } else {
                                alert("Not loaded image from canvas.toDataURL");
                            }
                        };
                        img.src = canvas.toDataURL("image/png");
                    }
            });
        });
    });
    

    See, this is absolute path mixed with "routes":

    GET http://127.0.0.1:5555/home/bhim/app/surveyApp_bhim/images/a0af53c02bd2f2aed37f1d895edcf3485117c512.png 404 (NOT FOUND) html2canvas.js:2249

    The route of proxy response for any reason is wrong, use this:

    1) Edit your code, like this:

    @surveyApp_module.route('/gpsDataMap/html2canvas-proxy')
    def html2canvas_proxy():
        print ("is this proxy really calling ");
        h2c = html2canvasproxy(request.args.get('callback'), request.args.get('url'))
        h2c.userAgent(request.headers['user_agent'])
    
        if request.referrer is not None:
            h2c.referer(request.referrer)
    
        if request.args.get('debug_vars'): #Added
            return Response((',\n'.join(h2c.debug_vars())), mimetype='text/plain') #Added
    
        h2c.route(real_path, virtual_path)
    
        r = h2c.result()
        return Response(r['data'], mimetype=r['mime'])
    

    2) Run in browser:

    http://127.0.0.1:5000/gpsDataMap/html2canvas-proxy?callback=console.log&url=http://www.google.com&debug_vars=1

    3) Get results and post in your queston.

    0 讨论(0)
  • 2021-01-15 18:18

    please try this first it may work for you.

    <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"></script> 
    <script type="text/javascript" src ="http://code.jquery.com/jquery-1.9.0.min.js"></script>
    <script type="text/javascript" src="html2canvas.js?rev032"></script> 
    
    
    
    <script type="text/javascript">
    
    function initialize()
        {
        var mapProp = {
              center:new google.maps.LatLng(51.508742,-0.120850),
              zoom:5,
              mapTypeId:google.maps.MapTypeId.ROADMAP
          };
        var map=new google.maps.Map(document.getElementById("googleMap"), mapProp);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    
     $(window).load(function(){
    
        $('#load').click(function(){
    
                html2canvas($('#googleMap'), {
                useCORS: true,
                    onrendered: function (canvas) {
                    var dataUrl= canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    
                    window.location.href = dataUrl;
                                        }
                });
    
        });
    });
    </script>
    </head>
    
    <body>
    <div id="googleMap" style="width:500px;height:380px;"></div>
    <input type="button" value="Save" id="load"/>
    </body>
    
    0 讨论(0)
  • 2021-01-15 18:22

    To save markers (workaround for html2canvas):

    (Source#1) : http://humaan.com/custom-html-markers-google-maps/
    (Source#2) : http://jsfiddle.net/BCr2B/99/

    Its quick and quite simple to implement your own markers, which then circumvents the tainted problems.

    function HTMLMarker(lat, lng, col) {
        this.lat = lat;
        this.lng = lng;
        this.col = col;
        this.pos = new google.maps.LatLng(lat, lng);
    }
    
    HTMLMarker.prototype = new google.maps.OverlayView();
    HTMLMarker.prototype.onRemove = function() {}
    HTMLMarker.prototype.onAdd = function() {}
    HTMLMarker.prototype.draw = function() {
        var self = this;
        var div = this.div;
    
        if(!div) {
            div = this.div = document.createElement('div');
            div.className = 'marker';
            div.style.position = 'absolute';
            div.style.width = '32px';
            div.style.height = '32px';
    
            switch(this.col) {
                case "green":
                    div.innerHTML = '<img src="/images/green-dot.png">';
                    break;
                case "yellow":
                    div.innerHTML = '<img src="/images/yellow-dot.png">';
                    break;
                case "red":
                    div.innerHTML = '<img src="/images/red-dot.png">';
                    break;
            }
    
            var panes = this.getPanes();
            panes.overlayImage.appendChild(div);
        }
    
    
        var position = this.getProjection().fromLatLngToDivPixel(this.pos);
        div.style.left = position.x - 16 + "px";
        div.style.top = position.y - 32 + "px";
    }
    

    Then just add:

    var htmlMarker = new HTMLMarker(data[gps].Latitude, data[gps].Longitude, "green");
    markersArray.push(htmlMarker.setMap(map));
    

    When you export in this way, the map markers are added successfully as they come from a local source.

    0 讨论(0)
提交回复
热议问题