为什么未在Google Maps(JavaScript)中显示此SVG?

你好,世界

我正在使用SVG文件在地图中显示图标,但其中一种特定的SVG无法正常工作。我已经测试了该文件,并且在浏览器中看起来不错,但是,在代码中执行该操作时,它什么也不显示,也不会失败:

let svgColor = 'red';
let strokeColor = "#00a0d6";

let innerSvg = '<path d="M19.64,16.41a2.07,2.07,0,1,1-2.06,2.06A2.07,2.07,0,0,1,19.64,16.41Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
innerSvg = innerSvg + '<path d="M6.25,16.41a2.07,2.07,0,1,1-2.06,2.06A2.07,2.07,0,0,1,6.25,16.41Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
innerSvg = innerSvg + '<path d="M9,8.17c-1.55,0-4.5,4.49-4.5,4.49S.68,13.93.68,15.79v2.69H3.44a1.23,1.23,0,0,1,0-.2,2.7,2.7,0,0,1,.29-1.07,2.81,2.81,0,0,1,5.32,1.27h7.78a2.81,2.81,0,0,1,5.31-1.27,2.58,2.58,0,0,1,.29,1,2.36,2.36,0,0,1,0,.25h1.61l.39-.53V8.15S10.51,8.18,9,8.17Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
innerSvg = innerSvg + '<path d="M19.64,19.6a1.13,1.13,0,1,0-1.12-1.13A1.13,1.13,0,0,0,19.64,19.6Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
innerSvg = innerSvg + '<path d="M6.25,19.6a1.13,1.13,0,1,0-1.12-1.13A1.12,1.12,0,0,0,6.25,19.6Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
innerSvg = innerSvg + '<path d="M6.26,12.26s2.2-3,2.84-3h3.23v3Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
var svg = '<svg height="40" width="150">' +
    innerSvg +
    '<text x="0" y="7" font-size="7pt" font-weight="bold" text-anchor="left" fill="black">Test</text>' +
    '</svg>';

const icon_ = {
    url: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg),
    anchor: new google.maps.Point(15, 30),
};
return icon_;

之前我听说过某些SVG复杂文件无法被Google Service识别,是这种情况吗?

我已经准备了一个jsfiddle测试:http : //jsfiddle.net/9u2kgxth/

代码段(来自小提琴):

var map;
var polyLine;
var polyOptions;
var rotation = 0;

function initialize() {

  var mapOptions = {
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: new google.maps.LatLng(0, 0)
  };

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  google.maps.event.addListener(map, 'click', function(event) {

    addPoint(event);
  });
}

function addPoint(event) {

  let svgColor = 'red';
  let strokeColor = "#00a0d6";

  let innerSvg = '<path d="M19.64,16.41a2.07,2.07,0,1,1-2.06,2.06A2.07,2.07,0,0,1,19.64,16.41Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M6.25,16.41a2.07,2.07,0,1,1-2.06,2.06A2.07,2.07,0,0,1,6.25,16.41Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M9,8.17c-1.55,0-4.5,4.49-4.5,4.49S.68,13.93.68,15.79v2.69H3.44a1.23,1.23,0,0,1,0-.2,2.7,2.7,0,0,1,.29-1.07,2.81,2.81,0,0,1,5.32,1.27h7.78a2.81,2.81,0,0,1,5.31-1.27,2.58,2.58,0,0,1,.29,1,2.36,2.36,0,0,1,0,.25h1.61l.39-.53V8.15S10.51,8.18,9,8.17Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M19.64,19.6a1.13,1.13,0,1,0-1.12-1.13A1.13,1.13,0,0,0,19.64,19.6Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M6.25,19.6a1.13,1.13,0,1,0-1.12-1.13A1.12,1.12,0,0,0,6.25,19.6Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M6.26,12.26s2.2-3,2.84-3h3.23v3Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  var svg = '<svg height="40" width="150">' +
    innerSvg +
    '<text x="0" y="7" font-size="7pt" font-weight="bold" text-anchor="left" fill="black">Test</text>' +
    '</svg>';

  const icon_ = {
    url: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg),
    anchor: new google.maps.Point(15, 30),
  };

  var marker = new google.maps.Marker({
    position: event.latLng,
    map: map,
    draggable: false,
    icon: icon
  });

  map.panTo(event.latLng);

  rotation += 10;
}

initialize();
body {
  font-family: Arial;
  font-size: 20px;
  line-height: 40px;
}

#map-canvas {
  height: 400px;
  width: 500px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
Click on map to add markers...
<div id="map-canvas"></div>

地理编码

<svg>缺少名称空间(如@RobertLongson在其评论中指出的那样):

var svg = '<svg height="40" width="150">' +
    innerSvg +
    '<text x="0" y="7" font-size="7pt" font-weight="bold" text-anchor="left" fill="black">Test</text>' +
    '</svg>';

应该:

var svg = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" height="40" width="150">' +
    innerSvg +
    '<text x="0" y="7" font-size="7pt" font-weight="bold" text-anchor="left" fill="black">Test</text>' +
    '</svg>';

概念证明

在此处输入图片说明

代码段:

var map;
var polyLine;
var polyOptions;
var rotation = 0;

function initialize() {

  var mapOptions = {
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: new google.maps.LatLng(0, 0)
  };
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  addPoint({
    latLng: map.getCenter()
  })

  google.maps.event.addListener(map, 'click', function(event) {

    addPoint(event);
  });
}

function addPoint(event) {

  let svgColor = 'red';
  let strokeColor = "#00a0d6";

  let innerSvg = '<path d="M19.64,16.41a2.07,2.07,0,1,1-2.06,2.06A2.07,2.07,0,0,1,19.64,16.41Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M6.25,16.41a2.07,2.07,0,1,1-2.06,2.06A2.07,2.07,0,0,1,6.25,16.41Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M9,8.17c-1.55,0-4.5,4.49-4.5,4.49S.68,13.93.68,15.79v2.69H3.44a1.23,1.23,0,0,1,0-.2,2.7,2.7,0,0,1,.29-1.07,2.81,2.81,0,0,1,5.32,1.27h7.78a2.81,2.81,0,0,1,5.31-1.27,2.58,2.58,0,0,1,.29,1,2.36,2.36,0,0,1,0,.25h1.61l.39-.53V8.15S10.51,8.18,9,8.17Z" style="fill:' + svgColor + ';stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M19.64,19.6a1.13,1.13,0,1,0-1.12-1.13A1.13,1.13,0,0,0,19.64,19.6Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M6.25,19.6a1.13,1.13,0,1,0-1.12-1.13A1.12,1.12,0,0,0,6.25,19.6Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  innerSvg = innerSvg + '<path d="M6.26,12.26s2.2-3,2.84-3h3.23v3Z" style="fill:#fff;stroke:' + strokeColor + ';stroke-miterlimit:1.0800000429153442;stroke-width:0.5400000214576721px"/>';
  var svg = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" height="40" width="150">' +
    innerSvg +
    '<text x="0" y="7" font-size="7pt" font-weight="bold" text-anchor="left" fill="black">Test</text>' +
    '</svg>';

  const icon_ = {
    url: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg),
    anchor: new google.maps.Point(15, 30),
  };
  var marker = new google.maps.Marker({
    position: event.latLng,
    map: map,
    draggable: false,
    icon: icon_
  });

  map.panTo(event.latLng);

  rotation += 10;
}

initialize();
body,
html {
  font-family: Arial;
  font-size: 20px;
  line-height: 40px;
  height: 100%;
  width: 100%;
}

#map-canvas {
  height: 90%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
Click on map to add markers...
<div id="map-canvas"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的可穿戴设备未在Google Fit API中列为数据源?

来自分类Dev

为什么未在Javascript中保存cookie?

来自分类Dev

为什么此代码未在“错误”事件中捕获错误?

来自分类Dev

为什么数组未在Ruby中修改?

来自分类Dev

为什么未在Express中设置cookie?

来自分类Dev

为什么表未在phpmyadmin中创建

来自分类Dev

为什么表未在mysql中更新

来自分类Dev

为什么未在JavaScript中悬挂回调函数?

来自分类Dev

为什么我的(javascript)更改事件未在测试中触发?

来自分类Dev

为什么递归函数内部循环未在javascript中完成

来自分类Dev

为什么递归函数内部循环未在javascript中完成

来自分类Dev

为什么此代码未在导航栏下划出边框?

来自分类Dev

为什么toGray功能未在设备上显示输出

来自分类Dev

为什么我的水印未在Java中心显示?

来自分类Dev

为什么JavaScript会显示此错误?

来自分类Dev

为什么此日期未在格林尼治标准时间显示?

来自分类Dev

为什么文件中的行未在Java中打印

来自分类Dev

如何捕获,为什么请求未在Spring Web中映射?

来自分类Dev

为什么未在定义中列出“ NOT(<search_condition>)”?

来自分类Dev

为什么未在页面源中添加元素

来自分类Dev

为什么UIView的框架未在ViewDidLayoutSubviews中更新?

来自分类Dev

为什么未在Python生成器中捕获异常?

来自分类Dev

为什么添加的模拟模块未在嵌套的beforeEach中执行

来自分类Dev

为什么未在此组件中设置我的状态

来自分类Dev

为什么我的通知事件未在Cordova中触发?

来自分类Dev

为什么未在Cloud Firestore中按顺序添加文档?

来自分类Dev

为什么Gradle未在intellij中列为项目选项

来自分类Dev

为什么该表格未在AntD中接收选择数据?

来自分类Dev

为什么循环时未在程序中定义屏幕?

Related 相关文章

  1. 1

    为什么我的可穿戴设备未在Google Fit API中列为数据源?

  2. 2

    为什么未在Javascript中保存cookie?

  3. 3

    为什么此代码未在“错误”事件中捕获错误?

  4. 4

    为什么数组未在Ruby中修改?

  5. 5

    为什么未在Express中设置cookie?

  6. 6

    为什么表未在phpmyadmin中创建

  7. 7

    为什么表未在mysql中更新

  8. 8

    为什么未在JavaScript中悬挂回调函数?

  9. 9

    为什么我的(javascript)更改事件未在测试中触发?

  10. 10

    为什么递归函数内部循环未在javascript中完成

  11. 11

    为什么递归函数内部循环未在javascript中完成

  12. 12

    为什么此代码未在导航栏下划出边框?

  13. 13

    为什么toGray功能未在设备上显示输出

  14. 14

    为什么我的水印未在Java中心显示?

  15. 15

    为什么JavaScript会显示此错误?

  16. 16

    为什么此日期未在格林尼治标准时间显示?

  17. 17

    为什么文件中的行未在Java中打印

  18. 18

    如何捕获,为什么请求未在Spring Web中映射?

  19. 19

    为什么未在定义中列出“ NOT(<search_condition>)”?

  20. 20

    为什么未在页面源中添加元素

  21. 21

    为什么UIView的框架未在ViewDidLayoutSubviews中更新?

  22. 22

    为什么未在Python生成器中捕获异常?

  23. 23

    为什么添加的模拟模块未在嵌套的beforeEach中执行

  24. 24

    为什么未在此组件中设置我的状态

  25. 25

    为什么我的通知事件未在Cordova中触发?

  26. 26

    为什么未在Cloud Firestore中按顺序添加文档?

  27. 27

    为什么Gradle未在intellij中列为项目选项

  28. 28

    为什么该表格未在AntD中接收选择数据?

  29. 29

    为什么循环时未在程序中定义屏幕?

热门标签

归档