map.jsx 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React from 'react';
  2. import loader from '@/components/AMap/loader';
  3. import geoData from './geoData';
  4. const dzData = geoData.features[0].geometry.coordinates[0][0];
  5. const plugins = ['AMap.ToolBar'];
  6. export default (props) => {
  7. const { style, className, onReady } = props;
  8. const containerRef = React.useRef();
  9. React.useEffect(() => {
  10. loader(plugins)
  11. .then((AMap) => {
  12. const map = new AMap.Map(containerRef.current, {
  13. mapStyle: 'amap://styles/d8278c296329ee0867aa26718e23bb80',
  14. layers: [
  15. // 卫星
  16. new AMap.TileLayer.Satellite(),
  17. // 路网
  18. new AMap.TileLayer.RoadNet(),
  19. ],
  20. zoom: 10,
  21. resizeEnable: true,
  22. center: [112.092716, 32.681642],
  23. });
  24. // 缩放按钮
  25. map.addControl(new AMap.ToolBar());
  26. // 绘制边线
  27. const dengzhou = new AMap.Polygon({
  28. path: dzData,
  29. fillColor: '#0050b3',
  30. strokeOpacity: 1,
  31. fillOpacity: 0.4,
  32. strokeColor: '#fff', // '#2b8cbe',
  33. strokeWeight: 1,
  34. strokeStyle: 'dashed',
  35. strokeDasharray: [5, 5],
  36. });
  37. map.add(dengzhou);
  38. onReady(map, AMap);
  39. })
  40. .catch((err) => {
  41. console.log(err.message);
  42. });
  43. }, []);
  44. return <div style={style} className={className} ref={containerRef}></div>;
  45. };