react-native-maps icon indicating copy to clipboard operation
react-native-maps copied to clipboard

Centered map after rotate that

Open bashirpour opened this issue 8 years ago • 6 comments

Environment

"react": "16.3.1",
"react-native": "0.55.3",
"react-native-google-places-autocomplete": "^1.3.6",
"react-native-map-clustering": "^1.3.0",
"react-native-maps": "*",
"react-native-svg": "^7.0.3",
"react-navigation": "^2.0.2",

Expected Behavior

Fixed maps after rotate

Actual Behavior

When i rotate the map and update region it automatically centered

Real Code

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

const {width, height} = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE_DELTA = 0.5;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;


type Props = {};
export default class App extends Component<Props> {


    onRegionChange(region) {
        this.setState({ region });
    }

    constructor(props){
        super(props);

        this.coordinate = {
            latitude: 35.688442,
            longitude: 51.403753,
            latitudeDelta: LATITUDE_DELTA,
            longitudeDelta: LONGITUDE_DELTA,
        };


        this.state = {
            region : this.coordinate
        }
    }

    render() {
        return (
            <MapView
                region={this.state.region}
                onRegionChangeComplete={this.onRegionChange.bind(this)}
                style={{...StyleSheet.absoluteFillObject}}
            />
            
        );
    }
}

Reproducible Demo

keth4-fxccc

bashirpour avatar Oct 03 '18 11:10 bashirpour

Please update your issue following the template or I'll close it

rborn avatar Oct 03 '18 11:10 rborn

I've updated my issue

bashirpour avatar Oct 03 '18 11:10 bashirpour

When you rotate the map is re-rendered so region will be reset. Try to use initialRegion={this.state.region}

rborn avatar Oct 03 '18 12:10 rborn

@rborn Thanks a lot

bashirpour avatar Oct 03 '18 13:10 bashirpour

When you rotate the map is re-rendered so region will be reset. Try to use initialRegion={this.state.region}

Is this really the expected behavior though? Per the docs it seems that initialRegion is only intended for use cases where the map will stay static, whereas region is intended for dynamic changes that involve user controls.

Rotation is most likely a key interaction along with drag and zoom, so whenever we expect a user to interact with the map, should we always be using initialRegion instead?

ejcho623 avatar Oct 23 '18 06:10 ejcho623

My solution for this issue is to use initialRegion for MapView, and use animateToRegion when the map view needs to be centered on a point of interest a user has just pressed.

FanchenBao avatar Jul 29 '21 00:07 FanchenBao