如何在 React Native 的 MapView 中设置标记

新手上路,请多包涵
  • 我想在React Native中的MapView上设置一个marker,但是在 MapView 的官方文档中找不到相关信息。
  • 如果不允许这样做,我如何在 React Native 中使用现有的反应模块,例如 react-googlemaps

原文由 ivan wang 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 461
2 个回答

您可以使用 annotations 设置标记。

例如:

 var markers = [
  {
    latitude: 45.65,
    longitude: -78.90,
    title: 'Foo Place',
    subtitle: '1234 Foo Drive'
  }
];

<MapView
  region={...}
  annotations={markers}
/>

原文由 naoufal 发布,翻译遵循 CC BY-SA 4.0 许可协议

谢谢@naoufal。最后,我能够在地图上显示标记以响应本机 IOS。

 <View style={styles.container}>
        <MapView style={styles.map}
          initialRegion={{
              latitude: 37.78825,
              longitude: -122.4324,
              latitudeDelta: 0.0,
              longitudeDelta: 0.0,
          }}
        >
        <MapView.Marker
            coordinate={{latitude: 37.78825,
            longitude: -122.4324}}
            title={"title"}
            description={"description"}
         />
      </MapView>
 </View>

对于地图和容器样式,我使用了以下样式:

  var styles = StyleSheet.create({

  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
    map: {
      position: 'absolute',
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
    }
});

我参考了以下链接: React native IOS- Display Markers on map

原文由 BK19 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题