在两个标记 map-box react-native 之间画一条线?
Draw a line between two markers map-box react-native?
我能够在 react-native
.
中使用以下代码在地图上创建 marker
(注释)
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import MapboxGL from '@mapbox/react-native-mapbox-gl';
import Mapbox from '@mapbox/react-native-mapbox-gl';
const columbusCircleCoordinates = [
-73.98197650909422, 40.768793007758816
];
Mapbox.setAccessToken('your access key');
export default class App extends Component {
renderAnnotations () {
return (
<Mapbox.PointAnnotation
key='pointAnnotation'
id='pointAnnotation'
coordinate={[11.254, 43.772]}>
<View style={styles.annotationContainer}>
<View style={styles.annotationFill} />
</View>
<Mapbox.Callout title='Look! An annotation!' />
</Mapbox.PointAnnotation>
)
}
render() {
return (
<View style={styles.container}>
<Mapbox.MapView
styleURL={Mapbox.StyleURL.Street}
zoomLevel={15}
centerCoordinate={[11.256, 43.770]}
showUserLocation={true}
style={styles.container}>
{this.renderAnnotations()}
</Mapbox.MapView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
annotationContainer: {
width: 30,
height: 30,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
borderRadius: 15,
},
annotationFill: {
width: 30,
height: 30,
borderRadius: 15,
backgroundColor: 'orange',
transform: [{ scale: 0.6 }],
}
});
但是从教程中我发现我们可以使用 <MapboxGL.LineLayer />
在 mapbox
上绘制 polylines
。但是有关于如何执行此操作的适当示例。
谁能给我一个示例 code
如何在 mapbox
react-native
.
上的两个注释之间绘制 line
就像我在之前的回答中分享的那样:在您的状态下,有一个变量是 linestring 类型的 geojson。这需要两个以上的坐标,这基本上是您通过该线的点数。 "awesome" mapbox 文档在向您显示折线标记时忽略提及的是,您需要将其包装在 MapboxGL 标记下的 shapeSource 标记中。在 this.state 中,我放置了一个名为 route 的 geojson 变量。也许使用以下代码示例会更有意义:
import React, {Component} from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import MapboxGL from '@mapbox/react-native-mapbox-gl';
MapboxGL.setAccessToken('your access key');
export default class App extends Component {
constructor() {
super();
this.state = {
route:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
11.953125,
39.436192999314095
],
[
18.896484375,
46.37725420510028
]
]
}
}
]
},
}
}
render() {
return (
<View style={styles.container}>
<MapboxGL.MapView
styleURL={MapboxGL.StyleURL.Light}
zoomLevel={15}
centerCoordinate={[11.256, 43.770]}
style={styles.container}>
<MapboxGL.ShapeSource id='line1' shape={this.state.route}>
<MapboxGL.LineLayer id='linelayer1' style={{lineColor:'red'}} />
</MapboxGL.ShapeSource>
</MapboxGL.MapView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
我能够在 react-native
.
marker
(注释)
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import MapboxGL from '@mapbox/react-native-mapbox-gl';
import Mapbox from '@mapbox/react-native-mapbox-gl';
const columbusCircleCoordinates = [
-73.98197650909422, 40.768793007758816
];
Mapbox.setAccessToken('your access key');
export default class App extends Component {
renderAnnotations () {
return (
<Mapbox.PointAnnotation
key='pointAnnotation'
id='pointAnnotation'
coordinate={[11.254, 43.772]}>
<View style={styles.annotationContainer}>
<View style={styles.annotationFill} />
</View>
<Mapbox.Callout title='Look! An annotation!' />
</Mapbox.PointAnnotation>
)
}
render() {
return (
<View style={styles.container}>
<Mapbox.MapView
styleURL={Mapbox.StyleURL.Street}
zoomLevel={15}
centerCoordinate={[11.256, 43.770]}
showUserLocation={true}
style={styles.container}>
{this.renderAnnotations()}
</Mapbox.MapView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
annotationContainer: {
width: 30,
height: 30,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
borderRadius: 15,
},
annotationFill: {
width: 30,
height: 30,
borderRadius: 15,
backgroundColor: 'orange',
transform: [{ scale: 0.6 }],
}
});
但是从教程中我发现我们可以使用 <MapboxGL.LineLayer />
在 mapbox
上绘制 polylines
。但是有关于如何执行此操作的适当示例。
谁能给我一个示例 code
如何在 mapbox
react-native
.
line
就像我在之前的回答中分享的那样:在您的状态下,有一个变量是 linestring 类型的 geojson。这需要两个以上的坐标,这基本上是您通过该线的点数。 "awesome" mapbox 文档在向您显示折线标记时忽略提及的是,您需要将其包装在 MapboxGL 标记下的 shapeSource 标记中。在 this.state 中,我放置了一个名为 route 的 geojson 变量。也许使用以下代码示例会更有意义:
import React, {Component} from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import MapboxGL from '@mapbox/react-native-mapbox-gl';
MapboxGL.setAccessToken('your access key');
export default class App extends Component {
constructor() {
super();
this.state = {
route:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
11.953125,
39.436192999314095
],
[
18.896484375,
46.37725420510028
]
]
}
}
]
},
}
}
render() {
return (
<View style={styles.container}>
<MapboxGL.MapView
styleURL={MapboxGL.StyleURL.Light}
zoomLevel={15}
centerCoordinate={[11.256, 43.770]}
style={styles.container}>
<MapboxGL.ShapeSource id='line1' shape={this.state.route}>
<MapboxGL.LineLayer id='linelayer1' style={{lineColor:'red'}} />
</MapboxGL.ShapeSource>
</MapboxGL.MapView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});