admin管理员组

文章数量:1291120

With OpenLayers 6.15.1, I can use the following code in my map options:

controls: ol.control.defaults({
    zoom: true,
    attribution: true,
    rotate: false
}),

With OpenLayers 7.1, this does not work anymore. I get an error:

Uncaught TypeError: ol.control.defaults is not a function

Does anyone could explain to me what I have to change in my code? I have found nothing explicit in the OpenLayers official documentation online about ol/control/defaults that could explain this error.

With OpenLayers 6.15.1, I can use the following code in my map options:

controls: ol.control.defaults({
    zoom: true,
    attribution: true,
    rotate: false
}),

With OpenLayers 7.1, this does not work anymore. I get an error:

Uncaught TypeError: ol.control.defaults is not a function

Does anyone could explain to me what I have to change in my code? I have found nothing explicit in the OpenLayers official documentation online about ol/control/defaults that could explain this error.

Share Improve this question edited May 24, 2023 at 11:54 geocodezip 161k14 gold badges226 silver badges255 bronze badges asked Sep 13, 2022 at 6:56 ThierryThierry 511 silver badge3 bronze badges 1
  • 1 try ol.control.defaults.defaults – BR75 Commented Sep 13, 2022 at 9:09
Add a ment  | 

1 Answer 1

Reset to default 12

See ol.interaction.defaults is not a function #14020 (and Legacy build 'control.defaults' and 'interaction.defaults' broken #14078)

from that issue:

  • ahocevar : It should be ol.interaction.defaults.defaults now.
  • hweri69 : Similarly it is now ol.control.defaults.defaults

The code was moved to its own moddule to resolve a circular dependency, see: Remove circular dependency #13967

(per ment by @MoonE)

code snippet:

//
// Create map, giving it a rotate to north control.
//

const map = new ol.Map({
  controls: ol.control.defaults.defaults({
    zoom: true,
    attribution: true,
    rotate: false
  }),
  layers: [
    new ol.layer.Tile({ // TileLayer({
      source: new ol.source.OSM(),
    }),
  ],
  target: 'map',
  view: new ol.View({
    center: [0, 0],
    zoom: 3,
    rotation: 1,
  }),
});
.map,
html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Default Controls</title>
  <script src="https://cdn.jsdelivr/npm/[email protected]/dist/ol.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr/npm/[email protected]/ol.css">
</head>

<body>
  <div id="map" class="map"></div>
  <!-- Pointer events polyfill for old browsers, see https://caniuse./#feat=pointer -->
  <script src="https://unpkg./[email protected]/dist/elm-pep.js"></script>
</body>

</html>

本文标签: javascriptolcontrolsdefault not a function in OpenLayers 71Stack Overflow