【问题标题】:Convert Google Maps polyline to SVG将 Google 地图折线转换为 SVG
【发布时间】:2017-10-09 12:48:22
【问题描述】:

我正在使用上一个问题 (Convert a Google Maps polygon path to an SVG path) 中的 JavaScript 来显示坐标列表中的 SVG。见 JsFiddle:http://jsfiddle.net/londonfed/9xhsL39u/51/

但是,这不能正确渲染(它显示一个红色斑点)。它应该显示一条直线,如下图所示:

上图取自https://developers.google.com/maps/documentation/utilities/polylineutility?csw=1

您可以将折线复制粘贴到编码的折线输入字段中,以查看 SVG 应如何呈现:

ofkyHluWg@?EyDQi@}ByFJMcAgCeAyCeAiDu@iC`@_@??a@^IY{@{Cu@aDSg@KQSq@m@kCYgAIc@QJKYc@{@W{@Ga@C_@SkBQcA??GaAMuASwAS_BGo@GWQ]c@i@GMKWGCCAUuA[sBSaBKm@E]OaB

非常感谢任何想法。

【问题讨论】:

    标签: javascript google-maps svg


    【解决方案1】:

    有几件事是错误的。主要是:

    • 您的坐标很小,因此默认笔划宽度“1”太大了。 0.0001 是您想要的更多比例。
    • 您需要的是开放路径,而不是封闭路径,因此不要包含“z”。

    function latLng2point(latLng) {
    
        return {
            x: (latLng.lng + 180) * (256 / 360),
            y: (256 / 2) - (256 * Math.log(Math.tan((Math.PI / 4) + ((latLng.lat * Math.PI / 180) / 2))) / (2 * Math.PI))
        };
    }
    
    function poly_gm2svg(gmPaths, fx) {
    
        var point,
        gmPath,
        svgPath,
        svgPaths = [],
            minX = 256,
            minY = 256,
            maxX = 0,
            maxY = 0;
    
        for (var pp = 0; pp < gmPaths.length; ++pp) {
            gmPath = gmPaths[pp], svgPath = [];
            for (var p = 0; p < gmPath.length; ++p) {
                point = latLng2point(fx(gmPath[p]));
                minX = Math.min(minX, point.x);
                minY = Math.min(minY, point.y);
                maxX = Math.max(maxX, point.x);
                maxY = Math.max(maxY, point.y);
                svgPath.push([point.x, point.y].join(','));
            }
    
    
            svgPaths.push(svgPath.join(' '))
    
    
        }
        return {
            path: 'M' + svgPaths.join(' M'),
            x: minX,
            y: minY,
            width: maxX - minX,
            height: maxY - minY
        };
    
    }
    
    function drawPoly(node, props) {
    
        var svg = node.cloneNode(false),
            g = document.createElementNS("http://www.w3.org/2000/svg", 'g'),
            path = document.createElementNS("http://www.w3.org/2000/svg", 'path');
        node.parentNode.replaceChild(svg, node);
        path.setAttribute('d', props.path);
        g.appendChild(path);
        svg.appendChild(g);
        svg.setAttribute('viewBox', [props.x, props.y, props.width, props.height].join(' '));
    
    
    }
    
    
    function init() {
    
        for (var i = 0; i < paths.length; ++i) {
            paths[i] = google.maps.geometry.encoding.decodePath(paths[i]);
        }
    
        svgProps = poly_gm2svg(paths, function (latLng) {
            return {
                lat: latLng.lat(),
                lng: latLng.lng()
            }
        });
        drawPoly(document.getElementById('svg'), svgProps)
    }
    
    //array with encoded paths, will be decoded later
    var paths = ["ofkyHluWg@?EyDQi@}ByFJMcAgCeAyCeAiDu@iC`@_@??a@^IY{@{Cu@aDSg@KQSq@m@kCYgAIc@QJKYc@{@W{@Ga@C_@SkBQcA??GaAMuASwAS_BGo@GWQ]c@i@GMKWGCCAUuA[sBSaBKm@E]OaB"];
    
    init();
    #svg {
        background:silver;
    }
    path {
        stroke: red;
        stroke-width: 0.0001;
        fill: none;
    }
    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
    <svg id="svg" height="400" width="400" viewport="0 0 400 400" preserveAspectRatio="xMinYMin meet"></svg>

    【讨论】:

    • 哇,保罗。这很有魅力,非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2020-06-16
    • 2012-05-16
    相关资源
    最近更新 更多