Get Directions between two points in Mapbox?(在MapBox中获取两个点之间的方向吗?)
本文介绍了在MapBox中获取两个点之间的方向吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近在REACT本机上使用MapBox gl,而不是使用Google地图, 我正在尝试添加一个要素,该要素在地图上显示从A点到B点的方向。或在我的Reaction本机应用程序中使用Mapbox directions API
以下是我尝试的代码,但在挂载屏幕后,应用程序成功崩溃:d
import MapboxGL from '@react-native-mapbox-gl/maps';
import React from 'react';
import {View} from 'react-native';
MapboxGL.setAccessToken(
'....',
);
class TwoPoints extends React.Component {
constructor(props) {
super(props);
this.featureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
id: '9d10456e-bdda-4aa9-9269-04c1667d4552',
properties: {
icon: 'example',
},
geometry: {
type: 'Point',
coordinates: [-73.989, 40.733],
},
},
{
type: 'Feature',
id: '9d10456e-bdda-4aa9-9269-04c1667d4552',
properties: {
icon: 'airport-15',
},
geometry: {
type: 'Point',
coordinates: [-74, 40.733],
},
},
{
type: 'Feature',
id: '9d10456e-bdda-4aa9-9269-04c1667d4552',
properties: {
icon: 'pin',
},
geometry: {
type: 'Point',
coordinates: [-74, 40.733],
},
},
],
};
}
render() {
return (
<View style={{flex: 1}}>
<MapboxGL.MapView
ref={c => (this._map = c)}
zoomEnabled={true}
style={{flex: 1}}>
<MapboxGL.ShapeSource shape={this.featureCollection}>
<MapboxGL.SymbolLayer
style={{iconColor: 'red'}}
minZoomLevel={25}
/>
</MapboxGL.ShapeSource>
</MapboxGL.MapView>
</View>
);
}
}
export default TwoPoints;
推荐答案
您可以这样从mapbox-gl-directions导入方向:
import React, { useState, useRef, useCallback } from 'react'
import MapGL from 'react-map-gl'
import Directions from 'react-map-gl-directions'
// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
const MAPBOX_TOKEN =
'pk.eyJ1IjoiaGVucmlxdWVub2JyZSIsImEiOiJja3dkZ3c2YmoydzdhMzBvMGRtdWVnd3J2In0.xdCkUviv0yGpX-t8L7ZKQ'
const MapBox = () => {
const [viewport, setViewport] = useState({
latitude: -16.6906,
longitude: -43.8054,
zoom: 8
})
const mapRef = useRef()
const handleViewportChange = useCallback(
(newViewport) => setViewport(newViewport),
[]
)
const handleGeocoderViewportChange = useCallback((newViewport) => {
const geocoderDefaultOverrides = { transitionDuration: 1000 }
return handleViewportChange({
...newViewport,
...geocoderDefaultOverrides
})
}, [])
return (
<div style={{ height: '100%' }}>
<MapGL
ref={mapRef}
{...viewport}
width="100%"
height="100%"
mapStyle="mapbox://styles/mapbox/streets-v11"
onViewportChange={handleViewportChange}
mapboxApiAccessToken={MAPBOX_TOKEN}
>
<Directions
mapRef={mapRef}
mapboxApiAccessToken={MAPBOX_TOKEN}
position="top-left"
unit="metric"
language="pt-BR"
/>
</MapGL>
</div>
)
}
export default MapBox
这篇关于在MapBox中获取两个点之间的方向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在MapBox中获取两个点之间的方向吗?
基础教程推荐
猜你喜欢
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01