首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在mapbox地图上绘制反应本地地图上的导航线?

如何在mapbox地图上绘制反应本地地图上的导航线?
EN

Stack Overflow用户
提问于 2020-10-26 15:44:58
回答 1查看 3.4K关注 0票数 9

我正在尝试使用mapbox从npm包获取导航指南:

“@mapbox/mapbox”:"^0.11.0“

为了呈现mapbox返回的说明,我使用以下npm包:

“@react本机-mapbox/map”:"^8.1.0-rc.8",

用于检索指示的代码:

代码语言:javascript
运行
复制
import MapboxGL from '@react-native-mapbox-gl/maps'
// Mapbox SDK related package
import MapboxDirectionsFactory from '@mapbox/mapbox-sdk/services/directions'
import { lineString as makeLineString } from '@turf/helpers'
import GeoLocationService from '../../services/geolocation/GeoLocationService';
import GeoLocationCore from '@react-native-community/geolocation'

const accessToken = "ACESS_TOKEN_FROM_MAPBOX_API_DASHBOARD"
const directionsClient = MapboxDirectionsFactory({accessToken})


  constructor(props) {
    super(props);
    this.state = {
        longitude: 0,
        latitude: 0,
        orderLongitude: 0,
        orderLatitude: 0,
        route: null,
    };
  }

async componentDidMount() {
      
      const {route} = this.props

      // Lets say route.params contains the below object:
      // { "longitude": "33.981982", "latitude": "-6.851599"}
      console.log("Params from other screen: ", route.params)

      MapboxGL.setAccessToken(accessToken)
      MapboxGL.setConnected(true);
      MapboxGL.setTelemetryEnabled(true);
      const permission = await MapboxGL.requestAndroidLocationPermissions();

      let latitude, longitude;
      if(Platform.OS == "android") {
          GeoLocationService.requestLocationPermission().then(() => {
            GeoLocationCore.getCurrentPosition(
                info => {
                    const { coords } = info
    
                    latitude = coords.latitude
                    longitude = coords.longitude
                    
                    //this.setState({longitude: coords.longitude, latitude: coords.latitude})
                    this.setState({longitude: -6.873795, latitude: 33.990777, orderLongitude: route.params.longitude, orderLatitude: route.params.latitude})
                    console.log("your lon: ", longitude)
                    console.log("your lat", latitude)
                    this.getDirections([-6.873795, 33.990777], [route.params.longitude, route.params.latitude])

                },
                error => console.log(error),
                {
                    enableHighAccuracy: false,
                    //timeout: 2000,
                    maximumAge: 3600000
                }
            )
        })
      }
  }

  getDirections = async (startLoc, destLoc) => {
      const reqOptions = {
        waypoints: [
          {coordinates: startLoc},
          {coordinates: destLoc},
        ],
        profile: 'driving',
        geometries: 'geojson',
      };
      const res = await directionsClient.getDirections(reqOptions).send()
      //const route = makeLineString(res.body.routes[0].geometry.coordinates)
      const route = makeLineString(res.body.routes[0].geometry.coordinates)
      console.log("Route: ", JSON.stringify(route))
      this.setState({route: route})
      
  }

用于绘制mapbox获取的道路方向的代码:

代码语言:javascript
运行
复制
  renderRoadDirections = () => {
    const { route } = this.state
    return route ? (
      <MapboxGL.ShapeSource id="routeSource" shape={route.geometry}>
        <MapboxGL.LineLayer id="routeFill" aboveLayerID="customerAnnotation" style={{lineColor: "#ff8109", lineWidth: 3.2, lineCap: MapboxGL.LineJoin.Round, lineOpacity: 1.84}} />
      </MapboxGL.ShapeSource>
    ) : null;
  };

用于绘制地图和指示的代码:

代码语言:javascript
运行
复制
render() {
   return (
       <View style={{ flex: 1 }}>
         <MapboxGL.MapView
                ref={(c) => this._map = c}
                style={{flex: 1, zIndex: -10}}
                styleURL={MapboxGL.StyleURL.Street}
                zoomLevel={10}
                showUserLocation={true}
                userTrackingMode={1}
                centerCoordinate={[this.state.longitude, this.state.latitude]}
                logoEnabled={true}
                >
                    {this.renderRoadDirections()}
            <MapboxGL.Camera
                zoomLevel={10}
                centerCoordinate={[this.state.longitude, this.state.latitude]}
                animationMode="flyTo"
                animationDuration={1200}
            />
        </MapboxGL.MapView>
       </View>
    )
}

现在,当我试图渲染GeoJson时,我撤回了地图上没有显示的道路方向线,所以我想我的GeoJson可能出了什么问题,并从这里测试了它,但它看起来很好:

https://geojsonlint.com/

我测试过的GeoJson,看起来还好:

代码语言:javascript
运行
复制
{"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":[[-6.880611,33.9916],[-6.882194,33.990166],[-6.882439,33.99015],[-6.882492,33.990028],[-6.882405,33.98991],[-6.878006,33.990299],[-6.87153,33.990978],[-6.871386,33.990925],[-6.871235,33.991016],[-6.869793,33.991165],[-6.870523,33.990292]]}}

我所努力达到的目标的例子:

在我的代码中,有什么可能是错误的,使道路方向线没有显示在地图上?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-28 01:59:07

找到导致地图上没有显示<LineLayer/>的原因,将属性aboveLayerID从下面的行中移除:

代码语言:javascript
运行
复制
<MapboxGL.LineLayer id="routeFill" aboveLayerID="customerAnnotation" style={{lineColor: "#ff8109", lineWidth: 3.2, lineCap: MapboxGL.LineJoin.Round, lineOpacity: 1.84}} /> 

因此,它变成:

代码语言:javascript
运行
复制
<MapboxGL.LineLayer id="routeFill" style={{lineColor: "#ff8109", lineWidth: 3.2, lineCap: MapboxGL.LineJoin.Round, lineOpacity: 1.84}} />

结果:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64540428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档