【问题标题】:React native maps MapView Region not updating on MarkerDrag反应原生地图 MapView 区域未在 MarkerDrag 上更新
【发布时间】:2020-12-11 08:01:30
【问题描述】:

我正在尝试实现在 react native 中搜索和查明位置。我正在使用 react-native-mapsreact-native-google-places-autocomplete 包,因为它们的明显用途。
首先,我在该州启动了区域:

   constructor(){
    this.state={
         mapRegion: {
                        latitude: this.props.latitude ? this.props.latitude : 27.7172,
                        longitude: this.props.longitude ? this.props.longitude : 85.3240,
                        latitudeDelta: 0.005,
                        longitudeDelta: 0.005,
                    },
    }
    }

我尝试在按下自动完成的搜索结果以及拖动标记时更新区域。但我得到一个错误:

Error: You attempted to set the key latitude with the value '27' on an object that is meant to be immutable and has been frozen. 

我已将代码实现为:

               <GooglePlacesAutocomplete
                            placeholder='Search'
                            fetchDetails={true}
                            onPress={(data, details = null) => {
                                let tempMapRegion = this.state.mapRegion;
                                tempMapRegion.latitude = details.geometry.location.lat;
                                tempMapRegion.longitude = details.geometry.location.lng;
                                this.setState({ mapRegion: tempMapRegion })
                            }}
                            query={{
                                key: AppSettings.googleMapApiKey,
                                language: 'en',
                            }}
                        />
                        <MapView
                            initialRegion={{
                                latitude: this.props.latitude ? this.props.latitude : 27.7172,
                                longitude: this.props.longitude ? this.props.longitude : 85.3240,
                                latitudeDelta: 0.005,
                                longitudeDelta: 0.005,
                            }}
                            region={this.state.mapRegion}
                            onRegionChange={(e) => { this.onRegionChange(e) }}
                            style={{ height: 300, width: 300 }}
                        >
                            <Marker draggable
                                coordinate={this.state.mapRegion}
                                onDragEnd={(e) => {
                                    let tempMapRegion = this.state.mapRegion;
                                    tempMapRegion.latitude = e.nativeEvent.coordinate.latitude
                                    tempMapRegion.longitude = e.nativeEvent.coordinate.longitude
                                    this.setState({ mapRegion: tempMapRegion })
                                }}
                            />
                        </MapView>

MapView中的onRegionChange运行流畅,marker会自动拖到中心,但反之则出现上述错误。

是什么导致了这个错误,我该如何解决这个问题?

【问题讨论】:

    标签: react-native google-maps react-native-maps


    【解决方案1】:
    <View style={{ padding: 2, }}>
                                <GooglePlacesAutocomplete
                                    placeholder='Search'
                                    fetchDetails={true}
                                    onPress={(data, details = null) => {
                                        let tempMapRegion = this.state.mapRegion;
                                        tempMapRegion.latitude = details.geometry.location.lat;
                                        tempMapRegion.longitude = details.geometry.location.lng;
                                        this.map.animateToRegion(this.newRegion(tempMapRegion));
                                        this.setState({ address: data.description })
    
                                    }}
                                    query={{
                                        key: AppSettings.googleMapApiKey,
                                        language: 'en',
                                    }}
                                />
                            </View>
                            <MapView
                                provider={this.props.provider}
                                ref={ref => { this.map = ref; }}
                                mapType={MAP_TYPES.TERRAIN}
                                initialRegion={this.state.mapRegion}
                                onRegionChangeComplete={(e) => { this.onRegionChange(e) }}
                                style={{ height: width, width: width, marginTop: -5 }}
                            >
                                <Marker draggable
                                    coordinate={this.state.mapRegion}
                                    onDragEnd={(e) => {
                                        let tempMapRegion = this.state.mapRegion;
                                        tempMapRegion.latitude = e.nativeEvent.coordinate.latitude
                                        tempMapRegion.longitude = e.nativeEvent.coordinate.longitude
                                        this.map.animateToRegion(this.newRegion(tempMapRegion));
                                        // this.setState({ mapRegion: tempMapRegion })
                                    }}
                                />
                            </MapView>
    

    所以,基本上是使用地图视图的animateToRegion 属性。它基本上将视图动画到提到的区域,然后调用onRegionChange。我曾多次偶然发现这个答案,但它没有奏效。奇怪的是,这只在构建版本中有效,在调试时无效,至少在模拟器上无效。

    感谢https://stackoverflow.com/a/53836679/5379191 这个答案为我们指明了方向。

       newRegion(tempMapRegion) {
            return {
                ...this.state.mapRegion,
                ...this.regionCoordinate(tempMapRegion),
            };
        }
    
        regionCoordinate(tempMapRegion) {
            return {
                latitude: tempMapRegion.latitude,
                longitude: tempMapRegion.longitude,
            };
        }
    

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      相关资源
      最近更新 更多