效果
源码
import { useState, useEffect, useRef } from 'react'
import L from 'leaflet'
// 不引入样式文件地图会错乱或不显示
import 'leaflet/dist/leaflet.css'
// 使用默认marker图标报错找不到,手动引入图片
import icon from 'leaflet/dist/images/marker-icon.png'
import iconShadow from 'leaflet/dist/images/marker-shadow.png'
const DefaultIcon = L.icon({
iconUrl: icon,
shadowUrl: iconShadow
})
// 挂在到默认属性
L.Marker.prototype.options.icon = DefaultIcon
const Leaflet = () => {
const [map, setMap] = useState()
const mapRef = useRef()
useEffect(() => {
if(map) return
// 创建地图
const tempMap = L.map(mapRef.current, {
// center:地图初始化时的中心点位置,坐标数组形式时,下标0是纬度,下标1是经度
center: [39,