【发布时间】:2019-12-10 11:32:15
【问题描述】:
我有两点我从数据库中得到它,我想渲染一条从起点到终点的线
我得到的是两点的直线,不考虑地图上的路线和方向
那么我该如何处理它以查看地图上的路线和方向?
这就是我得到的
这就是我的期望
这是我的代码
import MapboxGL from '@react-native-mapbox-gl/maps';
import React, {Component} from 'react';
import {PermissionsAndroid, StyleSheet, View} from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
export default class Mapbox extends Component {
constructor(props) {
super(props);
this.startPoint = [34.4999, 31.5542];
this.finishedPoint = [34.4979, 31.5512];
this.state = {
center: [],
// initialCoords: undefined,
initialCoords: [-77.034084, 38.9],
acceptedPermations: false,
// Two point state
route: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates: [
this.startPoint, //point A "current" ~ From
this.finishedPoint, //Point B ~ to
],
},
style: {
fill: 'red',
strokeWidth: '10',
fillOpacity: 0.6,
},
paint: {
'fill-color': '#088',
'fill-opacity': 0.8,
},
},
],
},
};
this.onRegionDidChange = this.onRegionDidChange.bind(this);
}
async componentDidMount() {
const isGranted = await MapboxGL.requestAndroidLocationPermissions();
this.setState({isGranted: isGranted});
MapboxGL.setAccessToken(
'....',
);
}
async onRegionDidChange() {
const center = await this._map.getCenter();
this.setState({center}, () =>
console.log('onRegionDidChange', this.state.center),
);
}
renderCurrentPoint = () => {
return (
<>
<MapboxGL.UserLocation
renderMode="normal"
visible={false}
onUpdate={location => {
const currentCoords = [
location.coords.longitude,
location.coords.latitude,
];
// console.log(location); // current location is here
this.setState({
initialCoords: currentCoords,
});
}}
/>
{/* current Provider location */}
<MapboxGL.PointAnnotation
selected={true}
key="key1"
id="id1"
coordinate={this.startPoint}>
<Icon name="ios-pin" size={45} color="#00f" />
<MapboxGL.Callout title="My" />
</MapboxGL.PointAnnotation>
{/* user From DB location */}
<MapboxGL.PointAnnotation
selected={true}
key="key2"
id="id2"
coordinate={this.finishedPoint}>
<Icon name="ios-pin" size={45} color="#0f0" />
<MapboxGL.Callout title="User" />
</MapboxGL.PointAnnotation>
<MapboxGL.ShapeSource id="line1" shape={this.state.route}>
<MapboxGL.LineLayer
id="linelayer1"
style={{
lineColor: 'red',
lineWidth: 10,
lineCap: 'round',
}}
/>
</MapboxGL.ShapeSource>
<MapboxGL.Camera
zoomLevel={16}
centerCoordinate={this.state.initialCoords}
// centerCoordinate={[-5.803457464752711, 35.769940811797404]}
/>
</>
);
};
render() {
if (!this.state.isGranted) {
return null;
}
return (
<View style={styles.page}>
<MapboxGL.MapView
styleURL={MapboxGL.StyleURL.Street}
ref={c => (this._map = c)}
onRegionDidChange={this.onRegionDidChange}
zoomEnabled={true}
style={styles.map}>
{this.renderCurrentPoint()}
</MapboxGL.MapView>
</View>
);
}
}
const styles = StyleSheet.create({
page: {
flex: 1,
// justifyContent: 'center',
// alignItems: 'center',
// backgroundColor: '#F5FCFF',
},
container: {
height: 500,
width: 500,
backgroundColor: 'tomato',
},
map: {
flex: 1,
},
});
【问题讨论】:
-
@Amalp 我根据他的回答实现了我的代码,但正如你在第一个屏幕截图中看到的那样,我很遗憾地得到了一条垂直线
-
没办法。如果您为 shape 属性提供一个坐标数组,它将不会创建直线。制作一个示例项目,并在您的代码出现问题时再次尝试他的答案。
-
@Amalp 嗯,很奇怪他的回答对你有用吗?我的意思是,正如您在第二张截图中看到的那样,它有一个真正的方向?
-
@DevAS,出现直线,是因为你只设置了2个坐标,起点和终点。尝试在 this.state..route..features..geometry..coordinates..startPoint、anotherPoint1、anotherPoint2、endPoint 中添加更多中间坐标。你明白我的想法了吗?
标签: javascript reactjs react-native mapbox mapbox-gl-js