首页 > 解决方案 > Mapbox 方向:出现错误 {"message":"Latitude must be between -90 and 90","code":"InvalidInput"}

问题描述

我正在构建一个网站应用程序,允许用户在地图上设置多个航点并计算完整路线并逐步给出方向。基本上,我正在遍历具有经度和纬度值的标记列表。我尝试替换示例经度和纬度值(-84.518641,39.134270;-84.512023,39.102779)并且它有效,但我的标记设置在澳大利亚,因此纬度超过 90。我尝试交换经度和纬度值但没有奏效任何一个。

当前出现错误:{“消息”:“纬度必须在 -90 和 90 之间”,“代码”:“无效输入”}

任何帮助将非常感激。

干杯!

下面的代码:

`const getRoute = (end) => {
      var start = [markers[0].lat,markers[0].lng];
      // var end = [markers[1].lat,markers[1].lng];
      console.log(start);
      console.log(end);
      var url = `https://api.mapbox.com/directions/v5/mapbox/walking/${start[0]},${start[1]};${end[0]},${end[1]}?steps=true&geometries=geojson&access_token=${mapboxgl.accessToken}`;
      

      // console.log(start);
      // // console.log(end);
      console.log(url);

    var req = new XMLHttpRequest();
      req.open('GET', url, true);
      req.onload = function() {
        var json = JSON.parse(req.response);
        var data = json.routes[0];
        var route = data.geometry.coordinates;
        var geojson = {
          type: 'Feature',
          properties: {},
          geometry: {
            type: 'LineString',
            coordinates: route
          }
        };
        // if the route already exists on the map, reset it using setData
        if (map.getSource('route')) {
          map.getSource('route').setData(geojson);
        } else { // otherwise, make a new request
          map.addLayer({
            id: 'route',
            type: 'line',
            source: {
              type: 'geojson',
              data: {
                type: 'Feature',
                properties: {},
                geometry: {
                  type: 'LineString',
                  coordinates: geojson
                }
              }
            },
            layout: {
              'line-join': 'round',
              'line-cap': 'round'
            },
            paint: {
              'line-color': '#3887be',
              'line-width': 5,
              'line-opacity': 0.75
            }
          });
        }
        // add turn instructions here at the end
      };
      req.send();
    // End of getInitRoute
    };
    getRoute([markers[1].lat,markers[1].lng]);

    map.on('load', function() {
      // make an initial directions request that
      // starts and ends at the same location
      getRoute(start);

      // Add starting point to the map
      map.addLayer({
        id: 'point',
        type: 'circle',
        source: {
          type: 'geojson',
          data: {
            type: 'FeatureCollection',
            features: [{
              type: 'Feature',
              properties: {},
              geometry: {
                type: 'Point',
                coordinates: start
              }
            }
            ]
          }
        },
        paint: {
          'circle-radius': 10,
          'circle-color': '#3887be'
        }
      });
      // this is where the code from the next step will go
    });
  // End of If statement
  }

// end of InitMapbox
};`

标签: javascriptmapboxlatitude-longitudemapbox-gl-js

解决方案


推荐阅读