react-native-maps: It does not work onPress props in Circle

Hi, my component code

<MapView region={this.state.region}>
<Circle center={this.state.region} onPress={()=>{ console.warn('xxxxxxxx!')}} radius={2000} />
</MapView> simple example, it does not work onPress in Circle bud move onPress in MapView and its ok

I require you to execute an event that returns latitude and longitude by tocuh in the circle marked on the map

react-native 0.43.4 react-native-maps 0.15.0

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 15
  • Comments: 15

Most upvoted comments

I solved my problem as follows:

First I installed geolib (https://github.com/manuelbieh/geolib) and I imported import {getDistance} from 'geolib';

I created the circles in my state and a control variable for the clicked circle

state = {
    _selectedZona: null,
    zones: [
        {
            _id: '123',
            radius: 1000,
            strokeWidth: 2,
            latitude: 37.78825,
            longitude: -122.4324
        },
        {
            _id: '1234',
            radius: 1000,
            strokeWidth: 2,
            latitude: 37.78829,
            longitude: -122.5329
        },
        {
            _id: '1235',
            radius: 1000,
            strokeWidth: 2,
            latitude: 37.78830,
            longitude: -122.6330
        }
    ]
}

then I used the MapView onPress event to retrieve the latitude and longitude clicked to compare with the latitudes and longitudes of my circles.

The calculation is simple: If the difference distance between the two points is less than the radius of my circle inside the loop, it means that it was clicked. At this point, store the _id of this circle in my state to do whatever it takes.

<MapView
    ref={component => this._mapView = component}
    style={styles.map}
    region={{
        latitude: 37.78825,
        longitude: -122.4324,
        latitudeDelta: 0.2,
        longitudeDelta: 0.2,
    }}
    onPress={(event) => {
        const coordinates = event.nativeEvent.coordinate;
        this.state.zones.map(zone => {
            const distance = getDistance(
                { latitude: coordinates.latitude, longitude: coordinates.longitude },
                { latitude: zone.latitude, longitude: zone.longitude }
            );

            if (distance <= zone.radius) {
                this.setState({
                    _selectedZona: zone._id
                })
            }
        })
    }}
>
    {this.state.zones.map(zone => (
            <Circle  
                key={zone._id}
                center={{
                    latitude: zone.latitude,
                    longitude: zone.longitude,
                }}
                radius={zone.radius}
                strokeWidth={zone.strokeWidth}
                strokeColor={ this.state._selectedZona == zone._id ? '#D63031' : '#4C4B5C'}
                fillColor={ this.state._selectedZona == zone._id ? 'rgba(255, 118, 117, 0.62)' : 'rgba(76,75,92,0.34)'}
            />
        ))
    }
</MapView>

I know there’s a work-around, but I’m wondering if there is any plan to fix the issue?

I solved my problem as follows:

First I installed geolib (https://github.com/manuelbieh/geolib) and I imported import {getDistance} from 'geolib';

I created the circles in my state and a control variable for the clicked circle

state = {
    _selectedZona: null,
    zones: [
        {
            _id: '123',
            radius: 1000,
            strokeWidth: 2,
            latitude: 37.78825,
            longitude: -122.4324
        },
        {
            _id: '1234',
            radius: 1000,
            strokeWidth: 2,
            latitude: 37.78829,
            longitude: -122.5329
        },
        {
            _id: '1235',
            radius: 1000,
            strokeWidth: 2,
            latitude: 37.78830,
            longitude: -122.6330
        }
    ]
}

then I used the MapView onPress event to retrieve the latitude and longitude clicked to compare with the latitudes and longitudes of my circles.

The calculation is simple: If the difference distance between the two points is less than the radius of my circle inside the loop, it means that it was clicked. At this point, store the _id of this circle in my state to do whatever it takes.

<MapView
    ref={component => this._mapView = component}
    style={styles.map}
    region={{
        latitude: 37.78825,
        longitude: -122.4324,
        latitudeDelta: 0.2,
        longitudeDelta: 0.2,
    }}
    onPress={(event) => {
        const coordinates = event.nativeEvent.coordinate;
        this.state.zones.map(zone => {
            const distance = getDistance(
                { latitude: coordinates.latitude, longitude: coordinates.longitude },
                { latitude: zone.latitude, longitude: zone.longitude }
            );

            if (distance <= zone.radius) {
                this.setState({
                    _selectedZona: zone._id
                })
            }
        })
    }}
>
    {this.state.zones.map(zone => (
            <Circle  
                key={zone._id}
                center={{
                    latitude: zone.latitude,
                    longitude: zone.longitude,
                }}
                radius={zone.radius}
                strokeWidth={zone.strokeWidth}
                strokeColor={ this.state._selectedZona == zone._id ? '#D63031' : '#4C4B5C'}
                fillColor={ this.state._selectedZona == zone._id ? 'rgba(255, 118, 117, 0.62)' : 'rgba(76,75,92,0.34)'}
            />
        ))
    }
</MapView>

Working from me… thanks!

Facing the same issue, any news on when Circle onPress will be fixed?

For anyone who wants a work around, you can use

<MapView onPress={event => checkCoords(event.nativeEvent.coordinate)} />

And then checkCoords iterates through your circles and uses the Haversine formula to check whether the press is inside the circle.

Explanation: https://en.wikipedia.org/wiki/Haversine_formula

Implementation: https://stackoverflow.com/questions/14560999/using-the-haversine-formula-in-javascript

Haversine is not perfect because it assumes the earth is a perfect sphere which introduces a whole bunch of problems but I’ve found it works fairly well. (And not triggering when tapping the edge of the circle isn’t too big a problem).

MapView.Circle onPress should definitely be fixed though.

The issue is 7 years old dudes… I don’t think the library is properly maintained anymore. It will probably not get fixed.

I guess so… Do you have a good alternative? Everywhere I go I sadly go back to here.

The issue is 7 years old dudes… I don’t think the library is properly maintained anymore. It will probably not get fixed.

Bump… Why isn’t anyone answering on this?