メインコンテンツまでスキップ

Draw a GeoJSON line

Load a polyline to a style using GeoJsonSource and display it on a map using LineLayer.
Android Examples App Available

This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.

See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.

DrawGeoJsonLineActivity.kt
package com.mapbox.maps.testapp.examples.linesandpolygons

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.maps.extension.style.layers.generated.lineLayer
import com.mapbox.maps.extension.style.layers.properties.generated.LineCap
import com.mapbox.maps.extension.style.layers.properties.generated.LineJoin
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
import com.mapbox.maps.extension.style.style

/**
* Load a polyline to a style using GeoJsonSource and display it on a map using LineLayer.
*/
class DrawGeoJsonLineActivity : AppCompatActivity() {

public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mapView = MapView(this)
setContentView(mapView)
mapView.mapboxMap.setCamera(
CameraOptions.Builder().center(
Point.fromLngLat(
LATITUDE,
LONGITUDE
)
).zoom(ZOOM).build()
)
mapView.mapboxMap.loadStyle(
(
style(style = Style.STANDARD) {
+geoJsonSource(GEOJSON_SOURCE_ID) {
data("asset://from_crema_to_council_crest.geojson")
}
+lineLayer("linelayer", GEOJSON_SOURCE_ID) {
lineCap(LineCap.ROUND)
lineJoin(LineJoin.ROUND)
lineOpacity(0.7)
lineWidth(8.0)
lineColor("#888")
}
}
)
)
}

companion object {
private const val GEOJSON_SOURCE_ID = "line"
private const val LATITUDE = -122.486052
private const val LONGITUDE = 37.830348
private const val ZOOM = 14.0
}
}
この{Type}は役に立ちましたか?