react native draw a circle on the map

Bisclavret picture Bisclavret · Apr 18, 2018 · Viewed 10.8k times · Source

This is probably pretty simple, but without anyone anywhere online providing an actual example of how to do this, I just can't get it working.

Here is my render() function, is this all I should need to do? :

render() {
    return (
    <MapContainer>
        <MapView.Circle
            center = {{ latitude: this.state.currentLatitude || 30, longitude: this.state.currentLongitude || 120 }}
            radius = { 1000 }
            strokeColor = "#4F6D7A"
            strokeWidth = { 2 }
        />
        <MapView 
            style = { styles.map }
            region = { this.state.mapRegion }
            showsUserLocation = { true }
            followUserLocation = { true }
            onRegionChangeComplete = { this.onRegionChangeComplete.bind(this) }>
        </MapView>
        <MessageBar />           
    </MapContainer>
    )
}

I have tried putting the MapView.Circle tag above and below the MapView tag, but it makes no difference.

Has anyone got this working?

Answer

Bisclavret picture Bisclavret · Apr 22, 2018

Here is the working code for anyone else that might be struggling with this:

RADIUS = 500;

class Map extends Component {

state = {
    mapRegion: null,
    currentLatitude: null,
    currentLongitude: null,
    LATLNG: {
        latitude: -35
        longitude: 120
    },
}

render() {
    return (
    <MapContainer>
        <MapView 
            style = { styles.map }
            region = { this.state.mapRegion }
            showsUserLocation = { true }
            followUserLocation = { true }
            onRegionChangeComplete = { this.onRegionChangeComplete.bind(this) }>
        <MapView.Circle
                key = { (this.state.currentLongitude + this.state.currentLongitude).toString() }
                center = { this.state.LATLNG }
                radius = { RADIUS }
                strokeWidth = { 1 }
                strokeColor = { '#1a66ff' }
                fillColor = { 'rgba(230,238,255,0.5)' }
                onRegionChangeComplete = { this.onRegionChangeComplete.bind(this) }
        />
        </MapView>
        <MessageBar />           
    </MapContainer>
    )
}