Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复map组件中心经纬度负数异常和scale非法值异常 #446

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/mini-program-example/src/pages/component/map/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class PageView extends React.Component {
}

handleClickLatitude = async () => {
const latitude = /^\d+(\.\d+)?$/.test(this.state.lat) ? this.state.lat : ''
const latitude = /^-?\d+(\.\d+)?$/.test(this.state.lat.toString()) || this.state.lat === 0 ? this.state.lat : ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非法值的异常是不是要在接口中判断,而不是在样例中判断

await this.setState({
latitude,
isShow: false,
Expand All @@ -182,7 +182,7 @@ export default class PageView extends React.Component {
}

handleClickLongitude = async () => {
const longitude = /^\d+(\.\d+)?$/.test(this.state.lng) ? this.state.lng : ''
const longitude = /^-?\d+(\.\d+)?$/.test(this.state.lng.toString()) || this.state.lng === 0 ? this.state.lng : ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

实现中有非法值校验吗

await this.setState({
longitude,
isShow: false,
Expand Down
51 changes: 35 additions & 16 deletions packages/taro-components/src/components/map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,39 +155,58 @@ export class Map implements ComponentInterface {
this.map.removeControl(this.map.getMapType())
// 创建中心点坐标对象
// 中心经纬度为必填值
if (this.latitude < -90 || this.latitude > 90 || this.longitude < -180 || this.longitude > 180 || !this.longitude || !this.latitude) {
if (this.latitude < -90 || this.latitude > 90 || this.longitude < -180 || this.longitude > 180 || isNaN(this.latitude) || isNaN(this.longitude)) {
console.error('请正确设置中心经纬度')
return
}
const minScale = this.minScale ? this.minScale : 3
const maxScale = this.maxScale ? this.maxScale : 20
let scale = isNaN(this.scale) ? 16 : this.scale
let minScale = isNaN(this.minScale) ? 3 : this.minScale
let maxScale = isNaN(this.maxScale) ? 16: this.maxScale
const point = new BMapGL.Point(this.longitude, this.latitude)
if (this.minScale < 3 || this.minScale > 20) {

if (minScale < 3 || minScale > 20) {
minScale = 3
this.map.setMinZoom(3)
} else {
// 设置最小缩放级别
this.map.setMinZoom(minScale)
this.map.setMinZoom(minScale)
}
if (this.maxScale > 20 || this.maxScale < 3) {

if (maxScale > 20 || maxScale < 3) {
maxScale = 20
this.map.setMaxZoom(20)
} else {
// 设置最大缩放级别
this.map.setMaxZoom(maxScale)
}
if (this.minScale > this.maxScale) {
this.map.setMinZoom(minScale)
this.map.setMaxZoom(maxScale)

if (minScale > maxScale) {
minScale = 3
this.map.setMinZoom(3)
maxScale = 20
this.map.setMaxZoom(20)
}

// 设置地图中心和缩放级别
if (this.scale >= minScale && this.scale <= maxScale) {
this.map.centerAndZoom(point, this.scale) // 使用this.scale来设置缩放级别
} else if (this.scale <= minScale) {
this.map.centerAndZoom(point, minScale)
} else if (this.scale >= this.maxScale) {
this.map.centerAndZoom(point, maxScale)
if (minScale <= 16 && maxScale >= 16) {
if (scale >= minScale && scale <= maxScale) {
// 使用this.scale来设置缩放级别
this.map.centerAndZoom(point, scale)
} else {
scale=16
this.map.centerAndZoom(point, scale)
}
} else {
this.map.centerAndZoom(point, 16)
if (scale >= minScale && scale <= maxScale) {
// 使用this.scale来设置缩放级别
this.map.centerAndZoom(point, scale)
} else if (scale < minScale) {
scale = minScale
this.map.centerAndZoom(point, minScale)
} else {
scale = maxScale
this.map.centerAndZoom(point, maxScale)
}
}

// 添加标记点markers
Expand Down
Loading