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

MapView.Circle don't update when coordinate changes #283

Closed
yonahforst opened this issue Jun 9, 2016 · 18 comments
Closed

MapView.Circle don't update when coordinate changes #283

yonahforst opened this issue Jun 9, 2016 · 18 comments

Comments

@yonahforst
Copy link

related to #187

I'm rendering my map like so:

                <MapView
                ref='mapView'
                style={{flex: 1}}
                onPress={this._onPress.bind(this)}
                followsUserLocation={true}
                showsCompass={true}
                showsUserLocation={true} >
                    {this.props.onlineUsers.map(u => Marker(u, this.props))}
                    {this.props.selectedUser ? Circle(this.props.selectedUser) : null}
                </MapView>

and corresponding circle class:

const Circle = user => {
    console.log(user.location.coords.longitude)
    return (
    <MapView.Circle
        center={user.location.coords}
        radius={user.location.coords.accuracy}
        strokeColor='transparent'
        fillColor={wRedLighter}/>
    )
}

the logs show that the coordinates are changing. but the circle remains in the same place until i remove it and re-add it.

@MarcusSjolin
Copy link

I get the same problem when I try to have a radius in state, and if I have + / - buttons to increase / decrease the radius value I can see it change, however the circle won't change on the map. Seems to not rerender itself when there's a change in props.

@shanedevane
Copy link

I have this issue on IOS but not android.

@ecjs
Copy link

ecjs commented Jun 19, 2016

I actually saw this issue myself a little while ago as well. Only tested this on iOS though.

@XavierColombel
Copy link

Same problem here on iOS simulator.

@yonahforst
Copy link
Author

The underlying problem is that iOS doesn't re-render an MKOverlay just because its properties change. A simple solution is to cause react to remove and re-add the element when size or position change (you won't able to animate the change, and there will be a slight flicker)

How does react decide if an element should be removed? I tried setting a key that was based on the circles position, hoping that react will think that this is a new element. but that didn't work.

@yonahforst
Copy link
Author

Ok, think i've got it. Pretty simple actually, just set the key to be a combination of the Circle's coordinate and radius.

I'm doing something like this:

const Circle = user => {
    return (
    <MapView.Circle
        key={(user.location.coords.longitude + user.location.coords.latitude + user.location.coords.accuracy).toString()}
        center={user.location.coords}
        radius={user.location.coords.accuracy}
        strokeColor='transparent'
        fillColor={wRedLighter}/>
    )
}

but you could also use some md5 or other hashing library to quickly calculate a hash for the entire object.

This causes the key to change every time the coordinates/radius changes, causing react to think it's a new element so the old one gets removed and the new one gets added

@XavierColombel
Copy link

Well done!

@ecjs
Copy link

ecjs commented Jun 22, 2016

Nailed it!

@felipegarcia92
Copy link

felipegarcia92 commented Aug 30, 2016

Thanks, it worked!

However, isn't it a work-around? Shouldn't this issue be opened still? It's expected that a Polygon like this one moves if the position passed as prop is changed.

@alvarolorentedev
Copy link
Contributor

i agree... this seems a workaround. and the change of center should be enough to update.

@yonahforst
Copy link
Author

@kanekotic - i'll reopen the issue, incase they want to put in a more permanent fix

@yonahforst yonahforst reopened this Feb 10, 2017
@alvarolorentedev
Copy link
Contributor

@yonahforst ok i am forking the repo to see if i can provide any fix for it :)

@NormalAndrew
Copy link

Clever workaround! Any permanent fix to this yet?

@neilakoh
Copy link

the key thing works like a charm :)

@doniyor2109
Copy link

really helped

@unmec
Copy link

unmec commented Jan 17, 2018

After twenty minutes debugging, I am glad I found this. :)
Thanks!

@lfkwtz
Copy link
Contributor

lfkwtz commented Sep 6, 2018

This looks like it was fixed in #2101

@rborn
Copy link
Collaborator

rborn commented Sep 7, 2018

Closing as per #283 (comment)

This issue was closed.
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