gpt4 book ai didi

android - "Error using newLatLngBounds(LatLngBounds, int): Map size can' t 为零...."在 Android 上使用 react-native-maps

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:07 27 4
gpt4 key购买 nike

我得到一个错误:“使用 newLatLngBounds(LatLngBounds, int) 时出错: map 大小不能为零。 map View 很可能尚未发生布局。等待布局发生或使用 newLatLngBounds(LatLngBounds, int, int, int) 允许您指定 map 的尺寸​​”。

但我为 getCurrentPosition 设置了一个警报,并且我正在从 getCurrentPosition() 接收坐标。

import React, { Component } from 'react';
import { View, Dimensions } from 'react-native';
import MapView from 'react-native-maps';


const {width, height} = Dimensions.get('window')

const SCREEN_HEIGHT = height
const SCREEN_WIDTH = width
const ASPECT_RATIO = width / height
const LATITUDE_DELTA = 0.0922
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO


class Map extends Component {

constructor(props) {
super(props)

this.state = {
isMapReady: false,
initialPosition: {
longitude: 0,
latitude: 0,
longitudeDelta: 0,
latitudeDelta: 0
},

markerPosition: {
longitude: 0,
latitude: 0
}

}
}

watchID: ?number = null

componentDidMount() {
navigator.geolocation.getCurrentPosition((position) => {

alert(JSON.stringify(position))

var lat = parseFloat(position.coords.latitude)
var long = parseFloat(position.coords.longitude)

var initialRegion = {
latitude: lat,
longitude: long,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}

this.setState({initialPosition: initialRegion})
this.setState({markerPosition: initialRegion})
},

(error) => alert(JSON.stringify(error)))

this.watchID = navigator.geolocation.watchPosition((position) => {
var lat = parseFloat(position.coords.latitude)
var long = parseFloat(position.coords.longitude)

var lastRegion = {
latitude: lat,
longitude: long,
longitudeDelta: LONGITUDE_DELTA,
latitudeDelta: LATITUDE_DELTA
}

this.setState({initialPosition: lastRegion})
this.setState({markerPosition: lastRegion})
})

}

componentWillUnmount() {
navigator.geolocation.clearWatch(this.watchID)
}

onMapLayout = () => {
this.setState({ isMapReady: true });
}

render() {

return (

<View style={styles.containerStyle}>
<MapView style={styles.mapStyle} initialRegion={this.state.initialPosition} onLayout={this.onMapLayout}>
{ this.state.isMapReady &&
<MapView.Marker coordinate={this.state.markerPosition}>
</MapView.Marker>
}
</MapView>
</View>

)

}

}

const styles = {
containerStyle: {
flex:1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'lightblue'
},

mapStyle: {
left: 0,
right: 0,
top: 0,
bottom: 0,
position: 'absolute'
}

}

export default Map;

老实说,我不知道出了什么问题……非常感谢您的帮助!谢谢!!

最佳答案

发生这种情况是因为 map View 尚未初始化。将调用移至 onMapLoaded 覆盖事件中。在你的

  @Override
public void onMapReady(GoogleMap googleMap)

添加:

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
//Your code where the exception occurred goes here
}
});

关于android - "Error using newLatLngBounds(LatLngBounds, int): Map size can' t 为零...."在 Android 上使用 react-native-maps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47726904/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com