【发布时间】: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