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



我正在使用 react-native-maps,我的地图显示,也有我的当前 纬度和经度,但我不知道如何用纬度和经度显示地图中的标记。

LAT和Long的代码:

callLocation(that){
navigator.geolocation.getCurrentPosition(
   (position) => {
      const currentLongitude = position.coords.longitude;
      const currentLatitude = position.coords.latitude;
      that.setState({ currentLongitude:currentLongitude });
      that.setState({ currentLatitude:currentLatitude });
   },
   (error) => alert(error.message),
   { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
that.watchID = navigator.geolocation.watchPosition((position) => {
    console.log(position);
    const currentLongitude = position.coords.longitude;
    const currentLatitude =  position.coords.latitude;
   that.setState({ currentLongitude:currentLongitude });
   that.setState({ currentLatitude:currentLatitude });
});}

地图代码:

<View style={styles.container}>
    <MapView
      style={styles.map}
      initialRegion={{
        latitude:  this.state.currentLatitude,
        longitude: this.state.currentLongitude,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      }}>
    </MapView>
  </View>

工作示例:

<MapView provider={Platform.OS === 'android' ? PROVIDER_GOOGLE : PROVIDER_DEFAULT}  // remove if not using Google Maps
 style={{width: '100%', height: FULL_SH*0.51, marginTop: 65*SH}}
  initialRegion={{
     latitude: this.state.myLat ? this.state.myLat : 38.4555,
     longitude: this.state.myLon ? this.state.myLon : 27.1199,
     latitudeDelta: 0.015,
     longitudeDelta: 0.0121}}>
    <MapView.Marker
       coordinate={{latitude: 38.4555, longitude: 27.1199}}
       title={'Deneme'}
     />
    <MapView.Marker
       onPress={() => this.setState({visible: true}) + setTimeout(() => alert(this.state.visible), 200)}
       description={'güzel mekan'}
       coordinate={{latitude: 38.4555, longitude: 27.1129}}
       title={'Deneme'}/>
</MapView>

您应该使用Marker组件。

示例:

import { MapView, Marker } from 'react-native-maps';
<MapView
  style={styles.map}
  initialRegion={{
    latitude:  this.state.currentLatitude,
    longitude: this.state.currentLongitude,
    latitudeDelta: 0.0922,
    longitudeDelta: 0.0421,
  }}
>
  <Marker
    coordinate={{latitude: 40.741075, longitude: 74.0003317}}
    title={'title'}
    description={'description'}
  />
</MapView>

相关内容

  • 没有找到相关文章

最新更新