English 中文(简体)
如何使用地图框仅显示MVT中点的位置标记名称?
原标题:How to only display Placemark names of points in MVT using mapbox?

我正在尝试使用Mapbox来显示矢量平铺集。矢量波浪集是从kml文件转换而来的。我使用gdal将其转换为MVT,它在Mapbox fine上显示,除了一件事:它还将所有lineString点的名称显示为标签。

我的kml文件中的lineString具有以下结构:

<Placemark>
   <name>mainlayer_0</name>
      <Style>
         <LineStyle><color>ffffffff</color><width>1</width></LineStyle>
      </Style>
      <OvStyle>
         <TrackStyle><type>5</type><width>79</width></TrackStyle>
      </OvStyle>
      <LineString>
         <coordinates>
            * many coordinates *
         </coordinates>
      </LineString>
</Placemark>

Mapbox将“mainlayer_0”显示为标签,并且在LineString中的每个坐标上都放置了相同的标签(每个坐标最多数百个),这是不希望的。我想显示的唯一标签是独立的点,具有以下结构:

<Placemark>
   <name>First Label Name</name>
   <description>I want to show this label</description>
   <Style>
      <IconStyle>
         <Icon>
         </Icon>
         <color>ffffffff</color>
         <scale>1.0</scale>
         </IconStyle>
            <LabelStyle>
               <color>ffffffff</color>
            </LabelStyle>
   </Style>
   <Point>
      <coordinates>/* coordinate of the point */ </coordinates>
   </Point>
</Placemark>

我首先做的是使用以下命令将kml文件转换为MVT:

ogr2ogr -f MVT output input.kml -dsco COMPRESS=NO -dsco MAXZOOM=15

并使用以下代码在地图框中显示MVT。lineLayer显示我需要显示的所有线条。在symbolLayer中,我使用[“get”,“Name”]来获取文本,但lineString名称也在“Name”字段中。我认为这就是问题所在,但我不知道如何解决它。

map.on("load", () => {
        map.addSource("map_name", {
          type: "vector",
          tiles: ["http://127.0.0.1:8080/output/{z}/{x}/{y}.pbf"],
          minzoom: 0,
          maxzoom: 15,
        });

        map.addLayer({
          id: "lineLayer",
          type: "line",
          source: "map_name",
          "source-layer": "mainlayer",
        });

        map.addLayer({
          id: "symbolLayer",
          type: "symbol",
          source: "map_name",
          "source-layer": "mainlayer",
          layout: {
            "text-field": ["get", "Name"],
            "text-anchor": "top",
          },
        });
      });

使用上面的代码,除了lineStrings的名称重复显示在它们的每个点上之外,一切看起来都很好。

我试图通过将所有的lineString名称替换为空字符串来修改kml文件,结果达到了预期效果。然而,我需要自动化这个过程,我不想每次都手动修改kml文件。当MapBox渲染标签时,我相信它知道这个点是一个点还是在LineString中,所以我的问题是:

  1. Is it possible to tell mapbox not to display any labels of a LineString?
  2. Is it possible to tell mapbox to only show name of points?

我还想知道在将kml转换为MVT的过程中,我是否可以做些什么来帮助解决这个问题。欢迎提出任何建议!

问题回答

这有点奇怪,因为我在发布问题五分钟后就找到了答案。答案是一行,并添加

filter: ["==", ["geometry-type"], "Point"],

map.addLayer({…})中。详细信息可在此处找到:设置筛选器

And an explained usage is here: Filter by geometry-type with new expressions

希望这能帮助将来的某个人^_^





相关问题
CLLocationManager Simulator to simulate car moves?

I m looking since quite a long time something like a CLLocationManager simulator that would enable me to simulate GPS positions (CLLocation instances that could be retrieved through the ...

Does GXml work with KMZ files?

Can Google Map API GXml parse .kmz files directly? if not how is the best way to convert .kmz file to .kml? The .kmz file is stored on database and PHP code is used to retrieve it.

Search engine optimization for kml file type

I ve a web site that generates kml files. An uri like this: /tokml?gid=2846 Generates a file like this: Mt. Dana Summit Trail.kml Using Header( Content-Disposition: inline; filename="Mt. Dana Summit ...

Silverlight create and execute file server-side

In my app i have the option of viewing a file that opens a window application (google earth). In order to do that i wish to create a custom file server-side under a certain relative path (eg. "//...

RSS to KML Overlay

I m want to display my blog as a Google Map overlay (each post contains geotags). How can I dynamically create a KML overlay from an RSS? Or better, how can I create a loop (PHP) that would display ...

热门标签