使用Mapbox和自定义样式在地图上画一条线

时间:2018-10-19 08:26:42

标签: android mapbox mapbox-android

我在使用LineLayer和自定义样式的Android上的Mapbox GeoJsonSource中绘制线条时遇到麻烦。

初始化地图后,我将创建一个GeoJsonSource,用几个点对其进行初始化,并将其链接到LineLayer。两者都添加到地图上,然后在地图上画一条线,并在上面画了几个点。当用户单击地图时,会将具有单击位置的点添加到点列表中,GeoJsonSource会更新,并且在地图顶部绘制的线也会更新以包括新点。在添加自定义样式之前,此方法一直有效:

map!!.setStyleUrl("mapbox://styles/xxx/yyy")

如果我使用通过Mapbox Studio创建的自定义样式(即使该样式只是未修改的基本样式)来初始化应用,则该应用最初不会显示该行(并且日志中没有错误),并且单击地图时发生崩溃(并且应该重新绘制线条)。

崩溃会产生以下日志输出:

2018-10-19 09:30:06.968 15000-15000/at.example.app A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x18 in tid 15000 (xample.app)
2018-10-19 09:30:07.022 15086-15086/? I/crash_dump32: obtaining output fd from tombstoned
2018-10-19 09:30:07.023 1494-1494/? I//system/bin/tombstoned: received crash request for pid 15000
2018-10-19 09:30:07.024 15086-15086/? I/crash_dump32: performing dump of process 15000 (target tid = 15000)
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.0.0/OSR1.170901.043/4456315:user/release-keys'
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: Revision: '0'
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: ABI: 'x86'
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: pid: 15000, tid: 15000, name: xample.app  >>> at.example.app <<<
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x18
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG: Cause: null pointer dereference
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG:     eax 00000004  ebx 88495b94  ecx 82f7f000  edx 82f7f000
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG:     esi 82f7f000  edi 00000000
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG:     xcs 00000073  xds 0000007b  xes 0000007b  xfs 0000003b  xss 0000007b
2018-10-19 09:30:07.025 15086-15086/? A/DEBUG:     eip 8812e7df  ebp bfa1ae08  esp bfa1ada0  flags 00010202
2018-10-19 09:30:07.026 15086-15086/? A/DEBUG: backtrace:
2018-10-19 09:30:07.026 15086-15086/? A/DEBUG:     #00 pc 0016d7df  /data/app/at.example.app-bbJXfFTgV6NlIR4r9mt_kw==/lib/x86/libmapbox-gl.so
2018-10-19 09:30:07.026 15086-15086/? A/DEBUG:     #01 pc 00170acb  /data/app/at.example.app-bbJXfFTgV6NlIR4r9mt_kw==/lib/x86/libmapbox-gl.so
2018-10-19 09:30:07.026 15086-15086/? A/DEBUG:     #02 pc 000382c8  /data/app/at.example.app-bbJXfFTgV6NlIR4r9mt_kw==/oat/x86/base.odex (offset 0x21000)
2018-10-19 09:30:07.026 15086-15086/? A/DEBUG:     #03 pc 000d9fff  [anon:libc_malloc:a5400000]
2018-10-19 09:30:07.026 15086-15086/? A/DEBUG:     #04 pc 01989717  /dev/ashmem/dalvik-main space (region space) (deleted)
2018-10-19 09:30:07.587 1494-1494/? E//system/bin/tombstoned: Tombstone written to: /data/tombstones//tombstone_01
2018-10-19 09:30:07.588 1663-15087/? W/ActivityManager:   Force finishing activity at.example.app/.MapsActivity

这是我活动的一个最小示例。我评论了我需要删除的行,以使应用程序正常工作。

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private var selectionSource: GeoJsonSource? = null
    private var selectionLayer: Layer? = null
    private val route = mutableListOf<Point>()

    override fun onMapReady(map: MapboxMap?) {
        if (map == null) return

        // The app works fine unless the style is loaded.
        map!!.setStyleUrl("mapbox://styles/xxx/yyy")

        map!!.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(47.0707, 15.4395), 11.0))

        map!!.addOnMapClickListener { latLng ->
            route.add(Point.fromLngLat(latLng.longitude, latLng.latitude))

         // Crash occurs on this line when using a custom style.
         selectionSource!!.setGeoJson(FeatureCollection.fromFeature(Feature.fromGeometry(LineString.fromLngLats(route))))
        }

        route.add(Point.fromLngLat(15.434503555297852, 47.078948480397834))
        route.add(Point.fromLngLat(15.441541671752928, 47.081461764162434))
        route.add(Point.fromLngLat(15.443687438964846, 47.07859778021446))

        val featureCollection = FeatureCollection.fromFeature(Feature.fromGeometry(LineString.fromLngLats(route)))
        selectionSource = GeoJsonSource("selection-source", featureCollection)
        map!!.addSource(selectionSource!!)

        selectionLayer = LineLayer("selection-layer", "selection-source")
        selectionLayer?.setProperties(
                PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
                PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
                PropertyFactory.lineWidth(5f),
                PropertyFactory.lineOpacity(0.5f),
                PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
        )
        map!!.addLayer(selectionLayer!!)
    }
}

我在这里错过了什么吗?使用自定义样式时是否需要特殊配置?任何帮助将不胜感激!

0 个答案:

没有答案