react使用地图

2024-01-08 16:49:49
 useEffect(() => {
        const loadAMap = () => {
          return new Promise((resolve, reject) => {
            if (window.AMap) {
              resolve(window.AMap);
            } else {
              const script = document.createElement('script');
              script.src = 'https://webapi.amap.com/maps?v=2.0&key=40335db62d1bb586ba8505e0c06ccac1';
              script.async = true;
              script.onerror = reject;
              script.onload = () => {
                resolve(window.AMap);
              };
              document.head.appendChild(script);
            }
          });
        };
        loadAMap().then((AMap) => {
            new AMap.Map('container', {
              viewMode: '2D',
              zoom: 17,
              center: [116.867982,36.683662 ]
            });
            
          }).catch((error) => {
            console.error('加载地图失败', error);
          })
      }, []);

文章来源:https://blog.csdn.net/weixin_63730900/article/details/135459707
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。