- 我想在 React Native 中的 MapView 上设置一个标记,但是通过MapView的官方文档找不到任何信息。
- 如果不允许以这种方式,我如何在 React Native 中使用现有的 react 模块,例如react-googlemaps ?
问问题
65999 次
8 回答
44
谢谢@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
于 2016-09-14T09:18:16.007 回答
29
您可以使用道具设置标记。annotations
例如:
var markers = [
{
latitude: 45.65,
longitude: -78.90,
title: 'Foo Place',
subtitle: '1234 Foo Drive'
}
];
<MapView
region={...}
annotations={markers}
/>
于 2015-05-24T15:56:50.773 回答
5
import MapView, { Marker } from 'react-native-maps';
<View>
<MapView
style={styles.mapStyle}
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
>
<Marker coordinate = {{latitude: 37.78825,longitude: -122.4324}}
pinColor = {"purple"} // any color
title={"title"}
description={"description"}/>
</MapView>
</View>
于 2020-03-12T06:49:52.203 回答
3
对于那些寻找动态(例如,来自 API)而不是硬编码坐标的人;这是自动生成的屏幕之一的复制/粘贴片段Expo CLI
,包括React Hooks
:
import axios from "axios";
import MapView from "react-native-maps";
import { Dimensions, StyleSheet } from "react-native";
import { Marker } from "react-native-maps";
import { Text, View } from "../components/Themed";
export default function TabTwoScreen() {
const [markers, setMarkers] = useState(null);
useEffect(() => {
axios({
method: "GET",
url: "https://API_URL...",
headers: {
Authorization: `Bearer MY_TOKEN_ABC123...`,
Accept: "application/json",
},
})
.then((response) => {
setMarkers(response.data.results);
})
.catch((error) => {
console.log(error);
});
}, []); // "[]" makes sure the effect will run only once.
return (
<View style={styles.container}>
<Text style={styles.title}>Tab Two</Text>
<MapView style={styles.map}>
{markers &&
markers.map((marker: any, index: number) => (
<Marker
key={index}
coordinate={{
latitude: marker.location[1],
longitude: marker.location[0],
}}
title={marker.title}
description={marker.description}
/>
))}
</MapView>
</View>
);
}
const styles = StyleSheet.create({
// Add your styles here...
});
于 2021-07-01T15:51:19.867 回答
2
更新代码 - 2020 | 反应原生 0.63
import MapView, { Marker } from "react-native-maps";
<MapView
style={styles.mapStyle}
region={{
latitude: this.state.mapLat,
longitude: this.state.mapLong,
latitudeDelta: 0.001663,
longitudeDelta: 0.002001,
}}
onRegionChangeComplete={this.onRegionChange}
>
<Marker
coordinate={{latitude: 51.5078788, longitude: -0.0877321}}
>
</Marker>
</MapView>
于 2020-11-13T20:09:40.817 回答
1
根据这个问题,它还没有被曝光。你也不能使用 web-React 包,React Native 不能那样工作。
两个建议:
- 等待上述问题得到解决以获取正确的 API
- 尝试使用 WebView 链接到带有注释的 Google 地图
我实际上不确定#2是否可能。
于 2015-04-02T12:49:47.983 回答
1
谢谢@zia_qureshi。最后,我能够在地图上显示标记以响应原生 android。
import MapView, { Marker } from "react-native-maps";
const App = () => {
const [region, setRegion] = useState({
latitude: 51.5078788,
longitude: -0.0877321,
latitudeDelta: 0.009,
longitudeDelta: 0.009
});
return (
<MapView
style={{ flex: 1 }}
region={region}
onRegionChangeComplete={region => setRegion(region)}
>
<Marker coordinate={{ latitude: 51.5078788, longitude: -0.0877321 }} />
</MapView>
);
};
export default App;
于 2020-08-18T07:43:19.523 回答
1
import MapView from 'react-native-maps';
<View style={StyleSheet.absoluteFillObject}>
<MapView style={styles.map}
showsUserLocation //to show user current location when given access
loadingEnabled //to show loading while map loading
style={styles.map}
initialRegion={{
latitude,
longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
>
{
locations && locations.map((location, index) => {
const {
coords: { latitude, longitude }
} = location;
return (
<MapView.Marker
key={index}
coordinate={{ latitude, longitude }}
title={"title"}
description={"address"}
// onPress={this.onMarkerPress(location)}
/>
)
})
}
</MapView>
</View>```
常量样式 = StyleSheet.create({
map: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}
});
“依赖”:{ “native-base”:“^2.13.8”,“react”:“16.9.0”,“react-native”:“0.61.2”,“react-native-maps”:“git+ https://git@github.com/react-native-community/react-native-maps.git " },
Try using this. Hope it helps to mark for many locations. Happy Coding.
于 2019-11-12T05:55:55.490 回答