3

Hye,Im新的反應本地化並想問我如何在地圖中渲染多個標記。在react-native-maps中渲染多個標記

這是我的代碼

內部類: -

constructor(props) { 
super(props); 

    this.state = { 
    coordinate: ([{ 
    latitude: 3.148561, 
    longitude: 101.652778, 
    title: 'hello' 
    }, 
    { 
    latitude: 3.149771, 
    longitude: 101.655449, 
    title: 'hello' 
    } 
    ]), 
}; 

}

內呈現: -

<MapView 
     style={styles.map} 
     showsUserLocation={true} 
     followUserLocation={true} 
     zoomEnabled={true} 
     //annotations={markers} 
     > 
      <MapView.Marker 
       coordinate={this.state.coordinate} 
       title={this.state.coordinate.title} 
      /> 
     </MapView> 

我想渲染裏面的地圖和我這兩個標記不知道如何在原生反應中做出循環來渲染它。我已經嘗試了文檔中的內容,但仍然無法工作。

預先感謝您:)

回答

14

coordinate屬性不正確構造。做這樣的事情 -

this.state = { 
    markers: [{ 
    title: 'hello', 
    coordinates: { 
     latitude: 3.148561, 
     longitude: 101.652778 
    }, 
    }, 
    { 
    title: 'hello', 
    coordinates: { 
     latitude: 3.149771, 
     longitude: 101.655449 
    }, 
    }] 
} 

內呈現

<MapView 
    .... 
> 
    {this.state.markers.map(marker => (
    <MapView.Marker 
     coordinate={marker.coordinates} 
     title={marker.title} 
    /> 
))} 
</MapView> 
+1

謝謝!在此之前,我誤解了如何構建座標的屬性,因爲我遵循react-native的MapView。再次感謝。你真的幫助我很多:)有一個美好的一天^ _^ –

+1

出於某種原因,這個確切的代碼不適合我。 –

+0

對我來說,我不得不添加一個'index'和'key'。 '{this.state.markers.map((標記指數)=>( \t \t \t \t \t \t \t \t \t \t))}'' –