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

Debug JS remotely on - map jumps to initialRegion when adding a new marker. #612

Closed
compojoom opened this issue Sep 26, 2016 · 1 comment

Comments

@compojoom
Copy link
Contributor

This thing is driving me crazy. I have this code:

<View style={styles.mapContainer}>
                    <MapView style={styles.map}
                             initialRegion={{
                                 latitude: this.state.position.lat,
                                 longitude: this.state.position.lng,
                                 latitudeDelta: 0.0922,
                                 longitudeDelta: 0.0421,
                             }}

                             onPress={(e) => this.onMapPress(e)}
                    >
                        {(() => {
                            if (this.state.marker) {
                                return <MapView.Marker
                                    key={this.state.marker.key}
                                    onDragEnd={(e) => this.onMarkerDragEnd(e)}
                                    coordinate={this.state.marker.coordinate}
                                    pinColor={this.state.marker.color}
                                    draggable
                                />
                            }
                        })()}
                    </MapView>
                </View>

and onMap press:

    onMapPress(e) {
        if(!this.state.marker)
        {
            this.setState({
                marker: {
                    coordinate: e.nativeEvent.coordinate,
                    key: id++,
                    color: randomColor(),
                },
            });
        }
    }

So basically when you click on the map i'm inserting a single marker. This works fine, however there is one issue. When I click on the map to insert the marker, the marker is inserted, but the map jumps to the original region.

But actually, if debug js remotely is turned off - the map doesn't jump to the initialRegion and behaves as expected. Any ideas?

@compojoom
Copy link
Contributor Author

Okay, After initialy losing half a day for this. Now 4mins after posting I discovered, that If I do what the examples are doing, then everything is fine. So I don't have to set initialRegion, but region and monitor the region change.

<View style={styles.mapContainer}>
                    <MapView style={styles.map}
                             region={this.state.region}
                             onRegionChange={this.onRegionChange.bind(this)}

                             onPress={(e) => this.onMapPress(e)}
                    >
                        {(() => {
                            if (this.state.marker) {
                                return <MapView.Marker
                                    key={this.state.marker.key}
                                    onDragEnd={(e) => this.onMarkerDragEnd(e)}
                                    coordinate={this.state.marker.coordinate}
                                    pinColor={this.state.marker.color}
                                    draggable
                                />
                            }
                        })()}
                    </MapView>
                </View>
onRegionChange(region) {
        this.setState({ region });
    }

Doing it like this obviously updates the region and even when the map rerenders - we are at the correct position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant