首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决使用tomtom地图的错误?

如何解决使用tomtom地图的错误?
EN

Stack Overflow用户
提问于 2022-04-04 19:25:32
回答 1查看 65关注 0票数 1

根据这个指南:https://developer.tomtom.com/blog/build-different/adding-tomtom-maps-django-app-building-front-end-map,我正在尝试使用tomtom作为我的django项目,一切都很好,除了缩放部分,地图应该缩放根据我给它的结果。我得到的consol日志错误是:

代码语言:javascript
复制
Uncaught (in promise) Error: `LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, an object {lon: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]

我试图运行的代码:

代码语言:javascript
复制
     // create the map
        tt.setProductInfo('Google Map', '1.0');
       let map = tt.map({
           key: '',
           container: 'map'
       });

       // add store markers
       let bounds = []
       let storeLocations = JSON.parse("{{ locations|escapejs }}");

       for (let storeLocation of storeLocations) {
           let coordinates = [storeLocation.longitude, storeLocation.latitude];
            bounds.push(coordinates);

           // create popup to display store information when the marker is clicked
           let popup = new tt.Popup().setHTML(`
               <div class="locator-popup">
                   <h6>Store Name</h6>
                   <p>${storeLocation.name}</p>
                   <h6>Address</h6>
                   <p>${storeLocation.address}</p>
               </div>
           `);

           let marker = new tt.Marker()
               .setLngLat(coordinates)
               .setPopup(popup)
               .addTo(map);
       }

       // zoom the map to fit all markers
        map.on('load', () => {
            console.log(bounds)
            map.fitBounds(bounds, {
               padding: { top: 50, bottom:50, left: 50, right: 50 }
           });
       })

注:我在想,这可能是因为“界限”。因此,检查了‘console.log(界)’的输出,结果是:

代码语言:javascript
复制
[Array(2)]
0: (2) [51.34912743809577, 35.701243277665895]
length: 1
lastIndex: (...)
lastItem: (...)
[[Prototype]]: Array(0)

在加载时,地图应该缩放。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-07 10:01:25

fitBounds()需要两个拐角-西南点和东北点。这个错误或多或少说明它没有得到LngLatLike参数。但事实上,它需要其中的两个。

因此,要修复代码--如果数组中只有一个存储,那么可以在边界数组中复制该位置。

类似于:

代码语言:javascript
复制
if (bounds.length = 1) {
  bounds.push(bounds[0])
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71742741

复制
相关文章

相似问题

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