"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = VmMap;
var _jquery = _interopRequireDefault(require("jquery"));
var _react = _interopRequireWildcard(require("react"));
var _mapStyle = require("./mapStyle");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
/* eslint-disable no-undef */

function VmMap(_ref) {
  var mapCenter = _ref.mapCenter,
    mapPoints = _ref.mapPoints;
  var mapRef = (0, _react.useRef)();
  var markers = (0, _react.useRef)([]);
  (0, _react.useEffect)(function () {
    initMap();
  }, []);
  (0, _react.useEffect)(function () {
    mapRef.current && addPoints(mapPoints);
  }, [mapPoints]);
  var initMap = function initMap() {
    _jquery["default"].getScript("//api.map.baidu.com/getscript?type=webgl&v=1.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ", function () {
      var center = mapCenter || [119.603701, 30.636529];
      mapRef.current = new window.BMapGL.Map('modalMap', {
        style: {
          styleJson: _mapStyle.customStyle
        }
      });
      var point = new window.BMapGL.Point(center[0], center[1]);
      // 设置中心点和层级
      mapRef.current.centerAndZoom(point, 15);
    });
  };
  // 地图添加点位
  var addPoints = function addPoints() {
    var points = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    removePoints();
    if (!(points === null || points === void 0 ? void 0 : points.length)) return;
    // 添加点位
    var positions = [];
    points.forEach(function (item) {
      var _a, _b;
      var position = new window.BMapGL.Point(item.lng, item.lat);
      positions.push(position);
      var icon = new window.BMapGL.Icon(item.url, new window.BMapGL.Size(item.width || 32, item.height || 45));
      var marker = new window.BMapGL.Marker(position, {
        icon: icon,
        offset: new window.BMapGL.Size(((_a = item.offset) === null || _a === void 0 ? void 0 : _a.width) || -10, ((_b = item.offset) === null || _b === void 0 ? void 0 : _b.height) || -16),
        title: item.name || ''
      });
      markers.current.push(marker);
      mapRef.current.addOverlay(marker);
    });
    mapRef.current.setViewport(positions);
  };
  // 清除地图点位
  var removePoints = function removePoints() {
    markers.current.forEach(function (marker) {
      mapRef.current.addOverlay(marker);
    });
    markers.current = [];
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    id: "modalMap",
    style: {
      width: '100%',
      height: '100%'
    }
  });
}
//# sourceMappingURL=index.js.map
