osmdroid学习(1)加载线面和地图

117 阅读1分钟

效果图:

效果图

完整代码:

主页MainActivity

class MainActivity : AppCompatActivity() {

    private var mapView: MapView? = null
    private var allBtn: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this))
        // Configuration.getInstance().isDebugMode = true
        setContentView(R.layout.activity_main)

        mapView = findViewById(R.id.map)
        allBtn = findViewById(R.id.all_btn)

        allBtn?.setOnClickListener {
            showAll()
        }

        initMap() // 如果权限已被授予,直接初始化地图
    }

    private fun showAll() {
        val allPoints = mutableListOf<GeoPoint>()

        // 收集所有覆盖物的点
        for (overlay in mapView!!.overlays) {
            when (overlay) {
                is Marker -> allPoints.add(overlay.position)
                is Polyline -> allPoints.addAll(overlay.points)
                is Polygon -> allPoints.addAll(overlay.points)
            }
        }

        if (allPoints.isNotEmpty()) {
            // 创建边界框并调整地图缩放
            val boundingBox = BoundingBox.fromGeoPoints(allPoints)
            mapView?.zoomToBoundingBox(boundingBox, true) // 调整地图以适应边界框
        }
    }

    override fun onResume() {
        super.onResume()
        mapView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView?.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView?.onDetach() // 解除地图的绑定
    }


    private fun initMap() {
        mapView?.setTileSource(MapBoxCustomTileSource())

        mapView?.controller?.setZoom(15.0)
        mapView?.controller?.setCenter(GeoPoint(48.8588443, 2.2943506)) // 设置中心点
        //启用缩放及滑动手势
        mapView?.setBuiltInZoomControls(true)
        mapView?.setMultiTouchControls(true)


        val marker = Marker(mapView)
        marker.position = GeoPoint(48.8588443, 2.2943506) // 标记位置
        marker.title = "埃菲尔铁塔"
        mapView?.overlays?.add(marker)


        val polygon = Polygon()
        val points = ArrayList<GeoPoint>()
        points.add(GeoPoint(48.8588443, 2.2943506))
        points.add(GeoPoint(48.859, 2.295))
        points.add(GeoPoint(48.857, 2.295))
        polygon.points = points
        polygon.fillColor = Color.argb(50, 255, 0, 0) // 填充颜色
        polygon.strokeColor = Color.RED // 边框颜色
        polygon.strokeWidth = 2f
        mapView?.overlays?.add(polygon)


        val polyline = Polyline()
        val points2 = ArrayList<GeoPoint>()
        // 添加折线的各个点
        points2.add(GeoPoint(48.8588443, 2.2943506)) // 第一个点
        points2.add(GeoPoint(48.859, 2.295)) // 第二个点
        points2.add(GeoPoint(48.857, 2.295)) // 第三个点
        polyline.setPoints(points2)
        polyline.color = Color.BLUE // 设置折线颜色
        polyline.width = 5f // 设置折线宽度
        // 将折线添加到地图覆盖层
        mapView?.overlays?.add(polyline)


        mapView?.invalidate()
    }

}

谷歌地图:GoogleMapCustomTileSource

import org.osmdroid.tileprovider.tilesource.XYTileSource
import org.osmdroid.util.MapTileIndex

class GoogleMapCustomTileSource : XYTileSource(
    "Custom Google Maps",
    0,
    18,
    256,
    ".png",
    arrayOf(
        "https://mt0.google.com/",
        "https://mt1.google.com/",
        "https://mt2.google.com/",
        "https://mt3.google.com/"
    )
) {

    override fun getTileURLString(pMapTileIndex: Long): String {
        return baseUrl + "vt/lyrs=m&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(
            pMapTileIndex
        ) + "&z=" + MapTileIndex.getZoom(pMapTileIndex)
    }
}

MapBox地图:MapBoxCustomTileSource

import org.osmdroid.tileprovider.tilesource.XYTileSource
import org.osmdroid.util.MapTileIndex

class MapBoxCustomTileSource : XYTileSource(
    "Mapbox",

    1,
    20,
    256,
    ".png",
    arrayOf("https://api.mapbox.com/styles/v1/{username}/{style_id}/tiles/256/")
) {

    private val username = "xxx"
    private val style_id = "xxx"
    private val MAPBOX_TOKEN =
        "pk.xxx.xxx"

    override fun getTileURLString(pMapTileIndex: Long): String {
        return baseUrl + MapTileIndex.getZoom(pMapTileIndex) + "/" + MapTileIndex.getX(pMapTileIndex) + "/" + MapTileIndex.getY(
            pMapTileIndex
        ) + "?access_token=" + "pk.xxx.xxx"
    }
}

参考文章:

官方地址: