React-native-maps: onPress event not firing on Polygons when using custom styling

Created on 2 Jul 2017  路  3Comments  路  Source: react-native-maps/react-native-maps

Hi! Great work on the library.

I want to draw one polygon for each country in a custom styled map and bind to the onPress event for adding interaction.

import React, { Component } from 'react';
import { Dimensions, Text, View } from 'react-native';
import { MapView } from 'expo';
import countries from './countries';
import mapStyle from './mapStyle';

const { width, height } = Dimensions.get('window');

export default class App extends Component {
  constructor() {
    super();

    let polygons = []
     countries.forEach((country) =>{
      country.polygons.forEach((p) => {
        polygons.push(p) 
      })
    })
    this.state = {
      region: {
        latitude: 24.889831,
        longitude: 67.0672087,
        latitudeDelta: 100,
        longitudeDelta: 100,
      },
      polygons: polygons
    }
  }

  onPolygonPress(polygon) {
    console.log('onPress');

  }

  render() {
    return (
      <View style={styles.container}>
        <MapView 
          provider={MapView.PROVIDER_GOOGLE}
          style={styles.map} 
          initialRegion={this.state.region}
          customMapStyle={mapStyle}
        >
          {this.state.polygons.map((polygon, index) => (
            <View key={index}>
              <MapView.Polygon
                coordinates={polygon}
                fillColor="rgba(255,255,255,0.5)"
                onPress={() => this.onPolygonPress(polygon)}
              />
            </View>
          ))}
        </MapView>
      </View>
    );
  }
}

const styles = {
  container: {
    alignItems: 'stretch',
    flex: 1,
  },
  map: {
    flex: 1,
  },
};

This is working ok except for the onPress event not firing on the polygons.
The weird thing is that if I remove this line provider={MapView.PROVIDER_GOOGLE} the onPress event starts working, but I lose the custom styling.

This is only occurring on IOS. If I remove the provider line and open the app in an android device it works with the styling and the onPress event.
Any ideas on how can I get both functionalities?

Cheers!

Most helpful comment

Hey @limoragni, so I just revisited the Polygon component API, and apparently there's a prop called "tappable" that enables onPress events, and it defaults to false on iOS. Just set it to true and it works! Not sure if I just missed it before or if it came with a recent update - hope this helps.

All 3 comments

Hi! Any updates on this? I'm having the same problem too.

Thanks!

Hey @limoragni, so I just revisited the Polygon component API, and apparently there's a prop called "tappable" that enables onPress events, and it defaults to false on iOS. Just set it to true and it works! Not sure if I just missed it before or if it came with a recent update - hope this helps.

@limoragni closing, this as @childv seems to have found the solution. Please ping me if you still have issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anandks1993 picture Anandks1993  路  3Comments

npomfret picture npomfret  路  3Comments

nbastoWM picture nbastoWM  路  3Comments

HiroNonoyama picture HiroNonoyama  路  3Comments

iSimar picture iSimar  路  3Comments