react-native-maps Marker的道具zIndex在iOS 11中无法正常工作 [英] react-native-maps Marker's prop zIndex doesn't work correctly in iOS 11

查看:65
本文介绍了react-native-maps Marker的道具zIndex在iOS 11中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 11中,通过更改状态来选择标记时,标记(react-native-maps)的zIndex道具不能正确工作,因为当我移动地图时,所选标记会传递到背景.仅当我按下标记时,效果很好.

In iOS 11 zIndex props of marker (react-native-maps) doesn't work correctly when I select a marker through changing the state because when I move the map the selected marker pass to background. Works good only when I press on the marker.

在Android或iOS 10、9、8中工作正常.

In Android or iOS 10,9,8 works fine.

黄色标记必须在前景中.

The marker yellow must be in foreground.

请,有人知道我该如何解决?

Please, somebody know how I can fix this?

我的代码:

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  Dimensions
} from 'react-native';
const { width, height } = Dimensions.get('window');
import MapView, { PROVIDER_GOOGLE, PROVIDER_DEFAULT } from 'react-native-maps';
const { Marker } = MapView;
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      index: 0
    };
    this.markers = [
      { id: 1, latitude: 43.152, longitude: -1.30051 },
      { id: 2, latitude: 43.1525, longitude: -1.30201 },
      { id: 3, latitude: 43.15264, longitude: -1.30025 },
      { id: 5, latitude: 43.1527, longitude: -1.30201 },
      { id: 6, latitude: 43.1525, longitude: -1.3001 },
      { id: 7, latitude: 43.1527, longitude: -1.30043 },
      { id: 8, latitude: 43.1526, longitude: -1.300531 },
      { id: 9, latitude: 43.1525, longitude: -1.30011 },
      { id: 10, latitude: 43.1521, longitude: -1.30062 },
      { id: 4, latitude: 43.152, longitude: -1.30083 }
    ];
  }
  render() {
    return (
      <View style={styles.container}>
        <MapView
          provider={PROVIDER_DEFAULT}
          style={{ flex: 1 }}
          initialRegion={{
            latitude: 43.153,
            longitude: -1.3,
            longitudeDelta: 20,
            latitudeDelta: 15
          }}
          minZoomLevel={15}
          ref={ref => { this.map = ref }}>
          {this.markers.map((marker, i) => {
            const selected = this.state.index == i;
            return (
              <Marker
                key={'m' + marker.id}
                coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
                onPress={() => this.setState({ index: i })}
                zIndex={selected ? 2 : 1}>
                <View style={[styles.marker, selected ? {zIndex:2} : {zIndex:1}]}>
                  <View style={styles.bubbleContainer}>
                    <View style={[styles.bubble, selected && styles.selected]}>
                      <Text style={styles.txt}>test</Text>
                    </View>
                  </View>
                </View>
              </Marker>
            );
          })}
        </MapView>
        <TouchableOpacity onPress={() => this.setState({ index: Math.round(Math.random() * 10) })}>
          <Text>Random select</Text>
        </TouchableOpacity>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    backgroundColor: '#fff',
    height
  },
  marker: {
    width: 50
  },
  bubbleContainer: {
    flexDirection: 'column',
    alignSelf: 'flex-start',
    backgroundColor: 'transparent',
    height: 45,
  },
  bubble: {
    flex: 0,
    flexDirection: 'row',
    alignSelf: 'flex-start',
    justifyContent: 'center',
    padding: 10,
    borderRadius: 10,
    borderColor: '#D1D1D1',
    borderWidth: 1,
    width: 49,
    backgroundColor: '#fff',
  },
  txt: {
    color: '#000',
  },
  selected: {
    backgroundColor: '#F7FF00'
  },
});

RN:0.48.4/0.50.4

RN: 0.48.4 / 0.50.4

RN映射:0.16.4/0.18.1

RN-maps: 0.16.4 / 0.18.1

OS:iOS 11/11.1

OS: iOS 11 / 11.1

推荐答案

我已经解决了编辑AIRMapMarker.m文件的问题.

I have solved the problem editing AIRMapMarker.m file.

我观察到,当您点击标记时,zIndex可以很好地工作.因此,我在C Swift代码中看到了setSelected方法,并在setZIndex中复制了该方法.

I observed that when you tap in a marker the zIndex works well. So I saw the method setSelected in the C Swift code and I replicate the method in setZIndex.

我写了一个条件:

- (void)setZIndex:(NSInteger)zIndex
{
    if (zIndex == 2) {  // added line
        [self setSelected:YES animated:NO]; // added line
    } else { // added line
        [self setSelected:NO animated:NO]; // added line
    } // added line
    _zIndex = zIndex;
    self.layer.zPosition = _zIndex;
}

它是硬编码的,但是可以工作.

it's hard coded but works.

这篇关于react-native-maps Marker的道具zIndex在iOS 11中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆