【问题标题】:Leaflet issues in ie8 and chrome errorsie8 中的传单问题和 chrome 错误
【发布时间】:2014-03-30 02:28:04
【问题描述】:

我正在尝试将此示例添加到内容管理系统中。一旦映射功能在所有 4 个浏览器中都可以使用,它就会被修改以满足我们的需求。不,我不能给你更多关于 CMS 的信息,因为它是私人的。请不要提及某些使用字符实体的代码。 ex

Ie8 声明 L 未定义。 Chrome 声称未捕获的引用错误 L 未定义。

不过 FF21 和 Safari 5.1.7 显示此示例 http://leafletjs.com/examples/choropleth.html

没有问题

为什么当示例在 CMS 中时,某些浏览器不会显示地图。这是演示中的糟糕编码吗?这是脚本加载方式的不同吗?解决此问题的最佳方法是什么?

由于 CMS,doctype 是框架集。

<html>
<head>
    <title>Leaflet Layers Control Example</title>


    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="http://leafletjs.com/examples/dist/leaflet.ie.css" /></link><![endif]-->

    <style>
        #map {
            width: 800px;
            height: 500px;
        }

        .info {
            padding: 6px 8px;
            font: 14px/16px Arial, Helvetica, sans-serif;
            background: white;
            background: rgba(255,255,255,0.8);
            box-shadow: 0 0 15px rgba(0,0,0,0.2);
            border-radius: 5px;
        }
        .info h4 {
            margin: 0 0 5px;
            color: #777;
        }

        .legend {
            text-align: left;
            line-height: 18px;
            color: #555;
        }
        .legend i {
            width: 18px;
            height: 18px;
            float: left;
            margin-right: 8px;
            opacity: 0.7;
        }
    </style>
</head>
<body>
    <div id="map"></div>

    <script src="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js"></script>

    <script type="text/javascript" src="http://leafletjs.com/examples/us-states.js"></script>
    <script type="text/javascript">




        var map = L.map('map').setView([37.8, -96], 4);

        var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
            attribution: 'Map data &#169; 2011 OpenStreetMap contributors, Imagery &#169; 2011 CloudMade',
            key: 'BC9A493B41014CAABB98F0471D759707',
            styleId: 22677
        }).addTo(map);


        // control that shows state info on hover
        var info = L.control();

        info.onAdd = function (map) {
            this._div = L.DomUtil.create('div', 'info');
            this.update();
            return this._div;
        };

        info.update = function (props) {
            this._div.innerHTML = '<h4>US Population Density</h4>' +  (props ?
                '<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
                : 'Hover over a state');
        };

        info.addTo(map);


        // get color depending on population density value
        function getColor(d) {
            return d > 1000 ? '#800026' :
                   d > 500  ? '#BD0026' :
                   d > 200  ? '#E31A1C' :
                   d > 100  ? '#FC4E2A' :
                   d > 50   ? '#FD8D3C' :
                   d > 20   ? '#FEB24C' :
                   d > 10   ? '#FED976' :
                              '#FFEDA0';
        }

        function style(feature) {
            return {
                weight: 2,
                opacity: 1,
                color: 'white',
                dashArray: '3',
                fillOpacity: 0.7,
                fillColor: getColor(feature.properties.density)
            };
        }

        function highlightFeature(e) {
            var layer = e.target;

            layer.setStyle({
                weight: 5,
                color: '#666',
                dashArray: '',
                fillOpacity: 0.7
            });

            if (!L.Browser.ie &amp;&amp; !L.Browser.opera) {
                layer.bringToFront();
            }

            info.update(layer.feature.properties);
        }

        var geojson;

        function resetHighlight(e) {
            geojson.resetStyle(e.target);
            info.update();
        }

        function zoomToFeature(e) {
            map.fitBounds(e.target.getBounds());
        }

        function onEachFeature(feature, layer) {
            layer.on({
                mouseover: highlightFeature,
                mouseout: resetHighlight,
                click: zoomToFeature
            });
        }

        geojson = L.geoJson(statesData, {
            style: style,
            onEachFeature: onEachFeature
        }).addTo(map);

        map.attributionControl.addAttribution('Population data &#169; <a href="http://census.gov/">US Census Bureau</a>');


        var legend = L.control({position: 'bottomright'});

        legend.onAdd = function (map) {

            var div = L.DomUtil.create('div', 'info legend'),
                grades = [0, 10, 20, 50, 100, 200, 500, 1000],
                labels = [],
                from, to;

            for (var i = 0; i &lt; grades.length; i++) {
                from = grades[i];
                to = grades[i + 1];

                labels.push(
                    '<i style="background:' + getColor(from + 1) + '"></i> ' +
                    from + (to ? '&#8211;' + to : '+'));
            }

            div.innerHTML = labels.join('<br />');
            return div;
        };

        legend.addTo(map);

    </script>
</body>
</html>

【问题讨论】:

    标签: javascript cross-browser leaflet


    【解决方案1】:

    CMS 可能在安全区域 (SSL / https://) 中运行,则不会加载不安全的 Leaflet .js 和 .css 资源 (http://)在大多数浏览器中,您会收到一些“L 未定义”错误消息。有些浏览器会加载不安全的元素。

    尝试从 https:// 位置包含传单资源。

    【讨论】:

      【解决方案2】:

      请不要提及某些使用字符实体的代码。 ex

      但当我禁止它们时,你的代码运行良好。

      <html>
      <head>
          <title>Leaflet Layers Control Example</title>
      
      
          <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.css" />
          <!--[if lte IE 8]><link rel="stylesheet" href="http://leafletjs.com/examples/dist/leaflet.ie.css" /></link><![endif]-->
      
          <style>
              #map {
                  width: 800px;
                  height: 500px;
              }
      
              .info {
                  padding: 6px 8px;
                  font: 14px/16px Arial, Helvetica, sans-serif;
                  background: white;
                  background: rgba(255,255,255,0.8);
                  box-shadow: 0 0 15px rgba(0,0,0,0.2);
                  border-radius: 5px;
              }
              .info h4 {
                  margin: 0 0 5px;
                  color: #777;
              }
      
              .legend {
                  text-align: left;
                  line-height: 18px;
                  color: #555;
              }
              .legend i {
                  width: 18px;
                  height: 18px;
                  float: left;
                  margin-right: 8px;
                  opacity: 0.7;
              }
          </style>
      </head>
      <body>
          <div id="map"></div>
      
          <script src="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js"></script>
      
          <script type="text/javascript" src="http://leafletjs.com/examples/us-states.js"></script>
          <script type="text/javascript">
      
      
      
      
              var map = L.map('map').setView([37.8, -96], 4);
      
              var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
                  attribution: 'Map data &#169; 2011 OpenStreetMap contributors, Imagery &#169; 2011 CloudMade',
                  key: 'BC9A493B41014CAABB98F0471D759707',
                  styleId: 22677
              }).addTo(map);
      
      
              // control that shows state info on hover
              var info = L.control();
      
              info.onAdd = function (map) {
                  this._div = L.DomUtil.create('div', 'info');
                  this.update();
                  return this._div;
              };
      
              info.update = function (props) {
                  this._div.innerHTML = '<h4>US Population Density</h4>' +  (props ?
                      '<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
                      : 'Hover over a state');
              };
      
              info.addTo(map);
      
      
              // get color depending on population density value
              function getColor(d) {
                  return d > 1000 ? '#800026' :
                         d > 500  ? '#BD0026' :
                         d > 200  ? '#E31A1C' :
                         d > 100  ? '#FC4E2A' :
                         d > 50   ? '#FD8D3C' :
                         d > 20   ? '#FEB24C' :
                         d > 10   ? '#FED976' :
                                    '#FFEDA0';
              }
      
              function style(feature) {
                  return {
                      weight: 2,
                      opacity: 1,
                      color: 'white',
                      dashArray: '3',
                      fillOpacity: 0.7,
                      fillColor: getColor(feature.properties.density)
                  };
              }
      
              function highlightFeature(e) {
                  var layer = e.target;
      
                  layer.setStyle({
                      weight: 5,
                      color: '#666',
                      dashArray: '',
                      fillOpacity: 0.7
                  });
      
                  if (!L.Browser.ie && !L.Browser.opera) {
                      layer.bringToFront();
                  }
      
                  info.update(layer.feature.properties);
              }
      
              var geojson;
      
              function resetHighlight(e) {
                  geojson.resetStyle(e.target);
                  info.update();
              }
      
              function zoomToFeature(e) {
                  map.fitBounds(e.target.getBounds());
              }
      
              function onEachFeature(feature, layer) {
                  layer.on({
                      mouseover: highlightFeature,
                      mouseout: resetHighlight,
                      click: zoomToFeature
                  });
              }
      
              geojson = L.geoJson(statesData, {
                  style: style,
                  onEachFeature: onEachFeature
              }).addTo(map);
      
              map.attributionControl.addAttribution('Population data &#169; <a href="http://census.gov/">US Census Bureau</a>');
      
      
              var legend = L.control({position: 'bottomright'});
      
              legend.onAdd = function (map) {
      
                  var div = L.DomUtil.create('div', 'info legend'),
                      grades = [0, 10, 20, 50, 100, 200, 500, 1000],
                      labels = [],
                      from, to;
      
                  for (var i = 0; i < grades.length; i++) {
                      from = grades[i];
                      to = grades[i + 1];
      
                      labels.push(
                          '<i style="background:' + getColor(from + 1) + '"></i> ' +
                          from + (to ? '&#8211;' + to : '+'));
                  }
      
                  div.innerHTML = labels.join('<br />');
                  return div;
              };
      
              legend.addTo(map);
      
          </script>
      </body>
      </html>
      

      在 chrome 上运行良好。 Try it yourself!

      【讨论】:

      • 压制他们是什么意思?请记住,如果您不使用字符代码,则会出现 cms 错误。
      • 我的意思是用它们常用的符号替换它们。代码很好,我认为问题来自您的 CMS。
      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多