如何在Mapbox中查询最接近地理编码结果的要素? [英] How can I query the feature that's closest to the geocode result in Mapbox?

查看:425
本文介绍了如何在Mapbox中查询最接近地理编码结果的要素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有数千个点的Mapbox地图.我希望用户能够键入一个地址和一个div,以填充从最接近该地址的信息.我正在使用mapbox-gl-geocoder控件

I'm trying to create a Mapbox map with thousands of points. I want the user to be able to type an address and a div to be filled with information from the closest point to that address. I'm using the mapbox-gl-geocoder control

如果我可以使用类似的东西,那将是完美的:

What would be perfect is if I could use something like:

geocoder.on('result', function(e) {
  var features = map.queryRenderedFeatures(e.result.center)
});

但是queryRenderedFeatures不接受latlong坐标,它需要视口坐标.

But queryRenderedFeatures doesn't accept latlong coordinates, it requires viewport coordinates.

我接下来尝试的是

map.on('moveend', function () {

    var xwindow = window.innerWidth / 2;
    var ywindow = window.innerHeight / 2;
    var xywindow = {'x': xwindow, 'y': ywindow};

    var features = map.queryRenderedFeatures(xywindow);
});

得出的结果接近查询的地址,但不是最接近的点.例如,如果我对知道某点存在的地址进行地址解析,则此查询将返回距离几条街的另一个点.

That gives results that are near the queried address, but not the closest point. For instance if I geocode an address where I know a point exists, this query will return a different point a few streets away.

是否可以使用queryRenderedFeatures在地理编码控件返回的位置上获取地图项?

Is there a way to use queryRenderedFeatures to get a feature on the location that is returned by the geocode control?

推荐答案

但是queryRenderedFeatures不接受latlong坐标,它需要视口坐标.

But queryRenderedFeatures doesn't accept latlong coordinates, it requires viewport coordinates.

是的,但是您可以使用 map.project 返回一个Point,该Point表示相对于地图容器的像素坐标,该像素坐标对应于指定的地理位置.

Yes but you can use map.project that Returns a Point representing pixel coordinates, relative to the map's container, that correspond to the specified geographical location.

一旦拥有所有功能,就需要与每个功能保持距离以找到最接近的功能. 价格便宜将是一个很好的选择.

Once you have all the features you'll need to get the distance to each of them to find the closest. cheap-ruler would be a good choice for that.

geokdbush 这样的索引在这里可能没有意义,因为您只运行了一次

An index like geokdbush probably doesn't make sense here as you're only running it once.

这篇关于如何在Mapbox中查询最接近地理编码结果的要素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆