我已经在片段中加载了Mapbox映射:

xml

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:mapbox_cameraZoomMax="@integer/maxZoom"
    mapbox:mapbox_cameraZoom="@integer/defaultZoom"
    mapbox:mapbox_cameraZoomMin="@integer/minZoom"
    mapbox:mapbox_uiRotateGestures="false"
    mapbox:mapbox_uiTiltGestures="false"
    mapbox:mapbox_uiScrollGestures="false"
    mapbox:mapbox_uiDoubleTapGestures="false" />

片段
    /** Initialise Mapbox **/
        mapView = view.findViewById(R.id.mapView)
        mapView?.onCreate(savedInstanceState)
        val destinationMarker = ContextCompat.getDrawable(activity, R.drawable.dest_logo) ?: return
        mapView?.getMapAsync { mapboxMap ->
            this.mapboxMap = mapboxMap
            this.mapboxMap.setStyle(Style.MAPBOX_STREETS) { style ->
                style.addImage("destination", destinationMarker)
                showUserLocation(style)
                resetCamera()
            }
        }

@SuppressWarnings("MissingPermission")
private fun showUserLocation(style: Style){
    // If permissions are granted, show/get user user1location. Else, return to TurnOnLocationActivity
    if (PermissionsManager.areLocationPermissionsGranted(activity)){
        val activity = activity ?: return
        val locationComponentOptions = LocationComponentOptions.builder(activity)
            .bearingTintColor(Color.WHITE)
            .accuracyAlpha(0.1f)
            .build()

        val locationComponentActivationOptions = LocationComponentActivationOptions
            .builder(activity, style)
            .locationComponentOptions(locationComponentOptions)
            .useDefaultLocationEngine(true)
            .build()
        val mapView = mapView ?: return returnToLoginPage()
        if (!style.isFullyLoaded) return returnToLoginPage()
        symbolManager = SymbolManager(mapView, mapboxMap, style)
        locationComponent = mapboxMap.locationComponent
        locationComponent?.activateLocationComponent(locationComponentActivationOptions)
        locationComponent?.isLocationComponentEnabled = true
        locationComponent?.cameraMode = CameraMode.TRACKING
        locationComponent?.renderMode = RenderMode.COMPASS
        return createLocationEngine()
    } else {
        Toast.makeText(context, "Permissions not granted", Toast.LENGTH_LONG).show()
        return returnToLocationPage()
    }

}

@SuppressWarnings("MissingPermission")
private fun createLocationEngine(){
    // Get current user1location
    val activity = activity ?: return
    locationEngine = LocationEngineProvider.getBestLocationEngine(activity)
    // After user1location has been loaded, configure mapBox settings
    mapboxMap.uiSettings.isCompassEnabled = false
}

我想在 map 上的2 Point之间绘制一条直线。假设我有-37.791890, 145.119387-37.790597, 145.116213作为我的2分-我将如何画一条直线?

编辑
FeatureCollection没有显示:
/** Initialise Mapbox **/
        mapView = view.findViewById(R.id.mapView)
        mapView?.onCreate(savedInstanceState)
        val destinationMarker = ContextCompat.getDrawable(activity, R.drawable.dest_logo) ?: return
        mapView?.getMapAsync { mapboxMap ->
            this.mapboxMap = mapboxMap
            this.mapboxMap.setStyle(Style.MAPBOX_STREETS) { style ->
                style.addImage("destination", destinationMarker)
                val routeCoordinates: List<Point> = listOf(Point.fromLngLat(145.152088, -37.759647))
                val lineString = LineString.fromLngLats(routeCoordinates)
                val feature = Feature.fromGeometry(lineString)
                val featureCollection = FeatureCollection.fromFeature(feature)
                val geoJsonSource = GeoJsonSource("line-source", featureCollection)
                style.addSource(geoJsonSource)
            }
        }

最佳答案

Line是Mapbox的Feature子类之一,因此添加Line的过程与任何其他功能几乎相同:

  • Point列表实例化LineString对象。 LineString是GeoJson
  • 的实现
  • 使用Feature对象
  • 实例化LineString
  • FeatureCollection对象
  • 实例化Feature
  • 使用GeoJsonSource实例化FeatureCollection
  • GeoJsonSource添加到 map 样式
  • 添加LineLayer以映射样式

  • List<Point> routeCoordinates;
    ...
    
    LineString lineString = LineString.fromLngLats(coordinates);
    Feature feature = Feature.fromGeometry(lineString);
    
    FeatureCollection featureCollection = FeatureCollection.fromFeature(feature);
    GeoJsonSource geoJsonSource = new GeoJsonSource("line-source", featureCollection);
    style.addSource(geoJsonSource);
    
    style.addLayer(new LineLayer("linelayer", "line-source").withProperties(
    PropertyFactory.lineDasharray(new Float[] {0.01f, 2f}),
    PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
    PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
    PropertyFactory.lineWidth(5f),
    PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
    ));
    

    这可以在map.setStyle() lambda内部完成。

    或者,如果您具有以下格式的json(LineString使用此格式):
     {
       "TYPE": "LineString",
       "coordinates": [
         [100.0, 0.0],
         [101.0, 1.0]
       ]
     }
    

    您可以跳到第三步,并从此json构造一个FeatureCollection:

    FeatureCollection.fromJson(stringJson)
    

    关于android - 如何在Mapbox map 上的2个点之间绘制一条直线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58912308/

    10-10 19:47