Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以在osmdroid中将可视区域设置为地图视图?_Android_Osmdroid - Fatal编程技术网

Android 是否可以在osmdroid中将可视区域设置为地图视图?

Android 是否可以在osmdroid中将可视区域设置为地图视图?,android,osmdroid,Android,Osmdroid,我有半透明的碎片覆盖在顶部和底部的地图视图上。因此,地图应在碎片下方可见,但对于居中项目,应忽略碎片覆盖的部分 在GoogleMaps SDK中,它是通过以下方法完成的: 设置地图上的填充 此方法允许您在地图上定义一个可见区域,通过在地图的四条边中的每一条边上设置填充,向地图发出信号,表明边缘周围的地图部分可能会被遮挡。贴图功能将根据填充进行调整。例如,缩放控件、指南针、版权声明和Google徽标将移动以适应定义的区域,相机移动将相对于可见区域的中心,等等 如何使用osmdroid完成类似的操作

我有半透明的碎片覆盖在顶部和底部的地图视图上。因此,地图应在碎片下方可见,但对于居中项目,应忽略碎片覆盖的部分

在GoogleMaps SDK中,它是通过以下方法完成的:

设置地图上的填充

此方法允许您在地图上定义一个可见区域,通过在地图的四条边中的每一条边上设置填充,向地图发出信号,表明边缘周围的地图部分可能会被遮挡。贴图功能将根据填充进行调整。例如,缩放控件、指南针、版权声明和Google徽标将移动以适应定义的区域,相机移动将相对于可见区域的中心,等等

如何使用osmdroid完成类似的操作


是否存在类似的方法?还是我需要自己实施?如果是这样的话,有人能告诉我现有的例子吗?

好的,我有一个@Sven实现

我有一个大头针放在一张地图的中心(正如你的问题所描述的),由于重叠的碎片,它有一个偏移量——见图1

当地图静止时(如果您感兴趣,可以使用
org.osmdroid.events.DelayedMapListener
),pin会“掉下来”,但为了将地图“中心”(用户看到的)与公开地图的顶部对齐,我已经覆盖了
MapView#getCenter()
调用,并使用了一个旧的Google Maps V1技巧“左上角和右下角[您]除以屏幕大小,得到每像素的值。”


没有API设置填充(…)那么性感,但效果也一样。

我创建了一个Kotlin扩展函数,它可以根据定义的填充调整

该函数使用初始边界框创建投影,并按每纬度/经度像素值调整边

您可以将其与以下组合使用:

职能:

fun BoundingBox.withOffset(
    mapView: MapView,
    @DimenRes top: Int,
    @DimenRes bottom: Int,
    @DimenRes left: Int,
    @DimenRes right: Int
): BoundingBox {
    val topPx = mapView.context.resources.getDimensionPixelSize(top)
    val bottomPx = mapView.context.resources.getDimensionPixelSize(bottom)
    val leftPx = mapView.context.resources.getDimensionPixelSize(left)
    val rightPx = mapView.context.resources.getDimensionPixelSize(right)

    val width = mapView.width
    val height = mapView.height

    val nextZoom = MapView.getTileSystem().getBoundingBoxZoom(
        this,
        width - (leftPx + rightPx),
        height - (topPx + bottomPx)
    )
    val projection = Projection(
        nextZoom, width, height,
        centerWithDateLine,
        mapView.mapOrientation,
        mapView.isHorizontalMapRepetitionEnabled, mapView.isVerticalMapRepetitionEnabled,
        mapView.mapCenterOffsetX, mapView.mapCenterOffsetY
    )

    val northWest = projection.fromPixels(0, 0)
    val southEast = projection.fromPixels(width, height)

    val lonPerPx = (southEast.longitude - northWest.longitude) / width
    val latPerPx = (southEast.latitude - northWest.latitude) / height

    return BoundingBox(
        latNorth - topPx * latPerPx,
        lonEast + rightPx * lonPerPx,
        latSouth + bottomPx * latPerPx,
        lonWest - leftPx * lonPerPx
    )
}

你是怎么上Sven的?想做一些类似的事情-考虑手动添加填充到OSM视图或做一些数学来移动地图中心,因为我需要它。
fun BoundingBox.withOffset(
    mapView: MapView,
    @DimenRes top: Int,
    @DimenRes bottom: Int,
    @DimenRes left: Int,
    @DimenRes right: Int
): BoundingBox {
    val topPx = mapView.context.resources.getDimensionPixelSize(top)
    val bottomPx = mapView.context.resources.getDimensionPixelSize(bottom)
    val leftPx = mapView.context.resources.getDimensionPixelSize(left)
    val rightPx = mapView.context.resources.getDimensionPixelSize(right)

    val width = mapView.width
    val height = mapView.height

    val nextZoom = MapView.getTileSystem().getBoundingBoxZoom(
        this,
        width - (leftPx + rightPx),
        height - (topPx + bottomPx)
    )
    val projection = Projection(
        nextZoom, width, height,
        centerWithDateLine,
        mapView.mapOrientation,
        mapView.isHorizontalMapRepetitionEnabled, mapView.isVerticalMapRepetitionEnabled,
        mapView.mapCenterOffsetX, mapView.mapCenterOffsetY
    )

    val northWest = projection.fromPixels(0, 0)
    val southEast = projection.fromPixels(width, height)

    val lonPerPx = (southEast.longitude - northWest.longitude) / width
    val latPerPx = (southEast.latitude - northWest.latitude) / height

    return BoundingBox(
        latNorth - topPx * latPerPx,
        lonEast + rightPx * lonPerPx,
        latSouth + bottomPx * latPerPx,
        lonWest - leftPx * lonPerPx
    )
}