【问题标题】:How to render a list of markers on a map?如何在地图上呈现标记列表?
【发布时间】:2020-04-14 15:06:04
【问题描述】:

我正在尝试在 RN 中使用 react-native-maps 呈现标记列表。

主要问题是如何获取坐标并使用映射来显示地图中的每个图钉。 下面我发布了到目前为止我尝试过的代码:

const { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE = 37.78825;
const LONGITUDE = -122.4324;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
const SPACE = 0.01;
const markerIDs = ['Marker1', 'Marker2', 'Marker3'];

    class map_of_patients extends React.Component {
        constructor(props) {
            super(props);

            this.state = {
                markers: [{
                    a: {
                        latitude: LATITUDE + SPACE,
                        longitude: LONGITUDE + SPACE,
                    },
                    b: {
                        latitude: LATITUDE - SPACE,
                        longitude: LONGITUDE - SPACE,
                    },
                    c: {
                        latitude: LATITUDE - SPACE * 2,
                        longitude: LONGITUDE - SPACE * 2,
                    }
                }]
            };
        }

        componentDidMount() {
            this.focus1();
        }

        focusMap(markers, animated) {
            this.map.fitToSuppliedMarkers(markers, animated);
        }

        focus1() {
            this.focusMap([markerIDs[0], markerIDs[2]], true);
        }

        render() {
            return (
                <View style={styles.container}>
                    <MapView
                        provider={this.props.provider}
                        ref={ref => {
                            this.map = ref;
                        }}
                        style={styles.map}
                        initialRegion={{
                            latitude: LATITUDE,
                            longitude: LONGITUDE,
                            latitudeDelta: LATITUDE_DELTA,
                            longitudeDelta: LONGITUDE_DELTA,
                        }}
                    >
                        {this.state.markers && this.state.markers.map(marker => (
                            <Marker
                                coordinate={marker.latlng}
                                title={marker.title}
                                description={marker.description}
                            />
                        ))}
                    </MapView>
                </View>
            );
        }
    }

我无法找到实际工作的东西,我尝试过的所有示例在我测试后都没有工作。

任何建议都将受到高度赞赏!

【问题讨论】:

    标签: react-native google-maps google-maps-markers react-native-maps


    【解决方案1】:

    多个标记的工作示例: https://github.com/WrathChaos/RN-MultipleMarkerMap-Example

    您的州标记图有误。 试试这个:

    this.state = {
          markers: [
            {
              latitude: LATITUDE + SPACE,
              longitude: LONGITUDE + SPACE,
            },
            {
              latitude: LATITUDE - SPACE,
              longitude: LONGITUDE - SPACE,
            },
            {
              latitude: LATITUDE - SPACE * 2,
              longitude: LONGITUDE - SPACE * 2,
            },
          ],
        };
    

    【讨论】:

    • 现在它的说法 latlng 不能为空 - 需要位置
    • 这是工作示例,请查看。 github.com/WrathChaos/RN-MultipleMarkerMap-Example
    • 如何从动态地图中获取坐标?我找到了一个很好的解决方案来放大点击坐标,但我想用一个动态的坐标数组来做
    猜你喜欢
    • 2022-01-10
    • 2022-01-11
    • 2021-09-10
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多