react native draw a circle on the map
Asked Answered
L

2

6

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?

Lowney answered 18/4, 2018 at 12:14 Comment(1)
Have I asked this incorrectly? Does no one understand what I am trying to do? I am not sure why no one is helping me. Does no one else know how to render a circle on a map either?Lowney
L
17

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>
    )
}
Lowney answered 22/4, 2018 at 10:27 Comment(2)
Complete code please including styles and MapConatinerCaritacaritas
What is the unit of the radius please? Km or meter?Brozak
P
2

Thank you so much for this! It saved me a lot of time. I was also having issues with adding Components in the MapView. Here's what I've come up with. (Just a simple example in case anyone needs it)

<View style={styles.container} >
  <MapView
      style={styles.map}
      initialRegion={{
        latitude: -29.1482491,
        longitude: -51.1559028,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      }}>
      <MapView.Circle
        center={{
          latitude: -29.1471337,
          longitude: -51.148951,
        }}
        radius={20}
        strokeWidth={2}
        strokeColor="#3399ff"
        fillColor="#80bfff"
      />
  </MapView>
  <View style={styles.allNonMapThings}>
      <View style={styles.inputContainer}>
        <TextInput
          placeholder=" Type some stuff!"
          style={ styles.input }
          keyboardType="numeric"
        />
      </View>

      <View style={styles.button} >
        <TouchableOpacity > 
          <Text style = {styles.buttonText} >
            Search
          </Text>
        </TouchableOpacity>
      </View>
    </View>
  </View>
Paramorphism answered 6/9, 2018 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.