【问题标题】:How to adjust position of a component that has been vertically and horizontally centered with `alignItems` and `justifyContent`?如何调整已使用 `alignItems` 和 `justifyContent` 垂直和水平居中的组件的位置?
【发布时间】:2020-09-26 00:59:07
【问题描述】:

我的目标是调整Icon 组件以指向准确的坐标。当前的解决方案确实将Icon 组件垂直和水平居中。但与Marker 组件相比,它不够精确。

当前的解决方案是使用top=-20 为 Icon 组件添加样式,但我认为这不是正确的解决方案。我相信top=-20 仅针对我的设备,不同的设备会导致不同的结果。

我尝试使用Marker 组件,但我还没有弄清楚如何防止每次状态更新时重新渲染。注意:不重新渲染标记不会改变位置。

import React from 'react';
import {StyleSheet, View} from 'react-native';
import MapView, {Marker, PROVIDER_GOOGLE} from 'react-native-maps';
import Icon from 'react-native-vector-icons/FontAwesome5';

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
    justifyContent: 'center',
    alignItems: 'center',
  },
  map: {
    ...StyleSheet.absoluteFillObject,
  },
});

export default function GoSendDestinationDetails() {
  let coordinate = {
    latitude: -6.1754,
    longitude: 106.8272,
    latitudeDelta: 0.015,
    longitudeDelta: 0.0121,
  };

  const handleRegionChange = (region) => {
    // console.log(region);
    coordinate = region;
  };

  // console.log('re-render');

  return (
    <View style={{height: '100%', width: '100%'}}>
      <View style={[styles.container, {height: '50%', width: '100%'}]}>
        <MapView
          provider={PROVIDER_GOOGLE} // remove if not using Google Maps
          style={styles.map}
          initialRegion={coordinate}
          onRegionChange={handleRegionChange}>
          <Marker coordinate={coordinate} />
        </MapView>
        {/* TODO: use gojek assets */}
        <Icon name="map-marker-alt" size={36} color="orange" />
      </View>
    </View>
  );
}

【问题讨论】:

    标签: javascript react-native react-native-maps


    【解决方案1】:

    您应该使用带有所需坐标的标记内的图标:

    <Marker coordinate={coordinate}>
      <Icon name="map-marker-alt" size={36} color="orange" />
    </Marker>
    

    这将使图标保持在您需要的位置。

    【讨论】:

    • 我的目标是在MapView 中放置一个垂直和水平居中的component。我知道更改标记坐标的方法需要re-render,在我的情况下会导致不必要的重新渲染。在 512 MB 的 Android 应用程序中使用时存在明显滞后。您知道无需重新渲染即可更改标记坐标的更好方法吗?
    • 完全不重新渲染就无法更改视图上的某些内容。您可以优化渲染以提高性能。渲染的哪一部分变慢了?
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 2015-08-29
    相关资源
    最近更新 更多