Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native draw a circle on the map

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?

like image 380
Bisclavret Avatar asked Apr 18 '18 12:04

Bisclavret


2 Answers

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>
    )
}
like image 93
Bisclavret Avatar answered Nov 13 '22 04:11

Bisclavret


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>
like image 2
ahcorso Avatar answered Nov 13 '22 04:11

ahcorso