TypeError: Cannot read properties of undefined (reading ‘lat‘)

const center = L.latLngBounds(markerBounds).getCenter(); // 获取闪烁圈的中心点

报错TypeError: Cannot read properties of undefined (reading 'lat')

这个错误通常是由于markerBounds未定义或者不包含有效的经纬度信息导致的。请确保markerBounds是一个有效的地理边界对象,并且包含了正确的经纬度信息。你可以通过打印markerBounds来检查它的值。

兼容这个问题,可在获取中心点之前先检查markerBounds是否已定义并且包含有效的经纬度信息。可使用条件语句来实现这个检查,例如:

if (markerBounds && markerBounds.isValid()) {
  const center = L.latLngBounds(markerBounds).getCenter();
  // 继续处理中心点
} else {
  // 处理未定义或无效的markerBounds情况
}

在这个示例中,使用了isValid()方法来检查markerBounds是否有效。如果markerBounds未定义或者无效,那么就会执行else语句中的代码,可在这里处理未定义或无效的markerBounds情况。

通过这种方式,可以避免在markerBounds未定义或无效时出现错误,并且可以根据实际情况进行处理。

以上报错TypeError: markerBounds.isValid is not a function

isValid()方法是用于Leaflet的LatLngBounds对象的,而不是普通的JavaScript对象。如果markerBounds是一个普通的JavaScript对象,可以使用以下代码进行兼容处理:

if (markerBounds && markerBounds.hasOwnProperty('lat') && markerBounds.hasOwnProperty('lng')) {
  // const center = L.latLng(markerBounds.lat, markerBounds.lng);
  const center = L.latLngBounds(markerBounds).getCenter(); // 获取闪烁圈的中心点
  // 继续处理中心点
} else {
  // 处理未定义或无效的markerBounds情况
}

在这个示例中,我们使用hasOwnProperty()方法来检查markerBounds对象是否包含latlng属性。如果markerBounds是一个有效的对象,并且包含了latlng属性,那么就可以使用这些属性来创建center对象。否则,可以在else语句中处理未定义或无效的markerBounds情况。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值