【问题标题】:mapsjs-core.js:27 Uncaught TypeError: Cannot read properties of undefined (reading '0')mapsjs-core.js:27 Uncaught TypeError:无法读取未定义的属性(读取“0”)
【发布时间】:2022-02-11 15:33:44
【问题描述】:

我需要帮助

我使用这里的地图 api 我尝试显示城市周围的轮廓,但这是一个错误

const shape = res.response.isoline[0].component[0].shape.map(z => z.split(','));

这是完整的代码

const requestIsolineShape = 选项 => { 常量参数 = { '模式':fastest;${options.mode};traffic:enabled, “开始”:geo!${options.center.lat},${options.center.lng}, “范围”:options.range, “范围类型”:options.rangeType, “出发”:${options.date}T${options.time}:00, };

return new Promise((resolve, reject) => { router.calculateIsoline( 参数, 水库=> { const shape = res.response.isoline[0].component[0].shape.map(z => z.split(',')); 解决(形状); }, 错误 => 拒绝(错误) ); }); } ;

【问题讨论】:

  • res.response.isoline 或 res.response.isoline[0].component 未定义
  • 谢谢@GoldenretriverYT,我刚看到(未捕获的类型错误:res.response.isoline 未定义)我该如何修复它

标签: javascript syntax-error here-api


【解决方案1】:

请在浏览器的控制台(网络选项卡)中仔细检查哪些参数被发送到路由隔离服务并检查响应。

您似乎在使用 Routing API v7.2,那么请在 https://refclient.ext.here.com/ 上玩游乐场 - 点击那里的“新标签 +”

如果您切换到新版本 8 的 Routing API 并使用直接集成到 JS API 的此服务,则更好的解决方案:https://developer.here.com/documentation/maps/3.1.30.7/api_reference/H.service.RoutingService8.html#calculateIsoline

例子:

let routingParams = {
 'routingMode': 'fast',
 'transportMode': 'car',
 'origin':'52.5,13.4',
 'range[type]': 'time',
 'range[values]': '900',
};

// Define a callback function to process the routing response
let onResult = function(result) {
  if (result.isolines.length) {
    result.isolines[0].polygons.forEach((polygon) => {
        // Create a linestring to use as a point source for the isoline
        const linestring = H.geo.LineString.fromFlexiblePolyline(polygon.outer);

        // Create a polygon and a marker representing the isoline
        const isolinePolygon = new H.map.Polygon(linestring, {
          style: { strokeColor: 'blue', lineWidth: 3 }
        });

        // Create a marker for the start point
        const isolineCenter = new H.map.Marker(result.departure.place.location);

        // Add the polygon and marker to the map
        map.addObjects([isolinePolygon, isolineCenter]);

        // Center and zoom the map so that the whole isoline polygon is
        // in the viewport
        map.getViewModel().setLookAtData({bounds: isolinePolygon.getBoundingBox()});
    });
  }
};

// Assumption: the platform is instantiated.
// Get an instance of the routing service
let router = platform.getRoutingService(null, 8);

// Call the Isoline Routing API to calculate an isoline
router.calculateIsoline(
  routingParams,
  onResult,
  console.error
);

v8 的游乐场:https://demo.routing.ext.here.com/

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2017-07-20
    • 1970-01-01
    • 2020-03-28
    • 2023-04-11
    • 2016-04-22
    相关资源
    最近更新 更多