-
是的,您可以使用<Marker/> 对象的image property 来更改标记的图标。如果您在使用 json 数据时有此信息,则可以放置一个条件语句来检查您将使用的图标类型。
-
如果您像下面的示例那样循环访问 json 文件,您可以使用 <CallOut /> 对象从 json 文件中显示您的标记信息。它将自动从 json 文件中为每个坐标创建的每个标记获取数据。
Sample code 和下面的代码 sn-p:
App.js
import React, { Component } from 'react';
import { Text, View, StyleSheet, Dimensions } from 'react-native';
import MapView, { Marker, Callout } from 'react-native-maps';
import data from './data.json';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
map: {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
},
callout: {
backgroundColor: 'white',
borderRadius: 4,
alignItems: 'center',
justifyContent: 'center',
padding: 4,
},
title: {
color: 'black',
fontSize: 14,
lineHeight: 18,
flex: 1,
},
});
class MapApp extends Component {
dragEnded = (e) => {
console.log("clicked");
};
render() {
return (
<View style={styles.container}>
<MapView
style={styles.map}
initialRegion={{
latitude: 40.756795,
longitude: -73.954298,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}>
{data.map((loc) => (
<Marker
coordinate={{ latitude: loc.lat, longitude: loc.lng }}
image={
loc.type == 'hospital'
? require('./assets/hosp.png')
: require('./assets/green.png')
}
description={loc.desc}
draggable
onDragEnd={this.dragEnded}>
<Callout tooltip style={styles.callout}>
<View>
<Text style={styles.title}>{loc.desc}</Text>
</View>
</Callout>
</Marker>
))}
</MapView>
</View>
);
}
}
export default MapApp;
data.json
[{
"desc": "This is Hospital",
"lat":40.756795,
"lng":-73.954298,
"type":"hospital"
},
{
"desc": "This is Green House",
"lat":40.753167,
"lng":-73.968120,
"type":"green"
}]