Custom SVG markers won't display in IE 11

前端 未结 3 825
误落风尘
误落风尘 2020-12-03 07:49

I\'m trying to display some bus routes using Google Data Layer, and then add some custom icon markers. Works great in Chrome and Firefox, but in IE

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 08:19

    OK! I done this in my web,I used it in my angularJS project, I share it use Jquery. I try two ways to create the custom google map marker, this run code use canvg.js is the best compatibility for browser.the Commented-Out Code is not support IE11 urrently("scaledSize" and "optimized: false" together seems did the tricky).

    var marker;
    var CustomShapeCoords = [16, 1.14, 21, 2.1, 25, 4.2, 28, 7.4, 30, 11.3, 30.6, 15.74, 25.85, 26.49, 21.02, 31.89, 15.92, 43.86, 10.92, 31.89, 5.9, 26.26, 1.4, 15.74, 2.1, 11.3, 4, 7.4, 7.1, 4.2, 11, 2.1, 16, 1.14];
    
    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13,
        center: {
          lat: 59.325,
          lng: 18.070
        }
      });
      var markerOption = {
        latitude: 59.327,
        longitude: 18.067,
        color: "#" + "000",
        text: "ha"
      };
      marker = createMarker(markerOption);
      marker.setMap(map);
      marker.addListener('click', changeColorAndText);
    };
    
    function changeColorAndText() {
      var iconTmpObj = createSvgIcon( "#c00", "ok" );
      marker.setOptions( {
                    icon: iconTmpObj
                } );
    };
    
    function createMarker(options) {
      //IE MarkerShape has problem
      var markerObj = new google.maps.Marker({
        icon: createSvgIcon(options.color, options.text),
        position: {
          lat: parseFloat(options.latitude),
          lng: parseFloat(options.longitude)
        },
        draggable: false,
        visible: true,
        zIndex: 10,
        shape: {
          coords: CustomShapeCoords,
          type: 'poly'
        }
      });
    
      return markerObj;
    };
    
    function createSvgIcon(color, text) {
      var div = $("
    "); var svg = $( '' + '' + '' + '' + text + '' + '' ); div.append(svg); var dd = $(""); var svgHtml = div[0].innerHTML; canvg(dd[0], svgHtml); var imgSrc = dd[0].toDataURL("image/png"); //"scaledSize" and "optimized: false" together seems did the tricky ---IE11 && viewBox influent IE scaledSize //var svg = '' // + '' // + '' // + '' + text + '' // + ''; //var imgSrc = 'data:image/svg+xml;charset=UTF-8,' + encodeURIComponent(svg); var iconObj = { size: new google.maps.Size(32, 43), url: imgSrc, scaledSize: new google.maps.Size(32, 43) }; return iconObj; };
    
    
    
    
      
      Your Custom Marker 
      
    
    
    
      

提交回复
热议问题