我想要实现的是,当userMarker在可见边界内完成一些操作时,这就是我的代码。
let screenWidth: Float = Float((map.frame.size.width))
let screenHeight: Float = Float((map.frame.size.height))
let minScreenPos: NTScreenPos = NTScreenPos(x: 0.0, y: 0.0)
let maxScreenPos: NTScreenPos = NTScreenPos(x: screenWidth, y:screenHeight)
let minPosWGS = projection.fromWgs84(map.screen(toMap: minScreenPos))!
let maxPosWGS = projection.fromWgs84(map.screen(toMap: maxScreenPos))!
let mapBounds = NTMapBounds(min: minPosWGS, max: maxPosWGS)
let markerCenter = projection.fromWgs84(userMarker.getBounds().getCenter())
let markerBounds = userMarker.getBounds()
let containPos = mapBounds!.contains(markerCenter)
let containBounds = mapBounds!.contains(markerBounds)
print(containPos)
print(containBounds)但是输出总是错误的,我做错了什么,任何帮助,请。
发布于 2017-11-28 17:10:06
嗨@Nikitah我最终得到了这个解决方案
我在MapEventsListener和那里实现MapEventsListener事件--我要求这样做
if latestLocation != nil {
delegate?.hideLocationButton()
}所以在mi hideLocationButton方法中我这样做
let screenWidth: Float = Float(map.frame.width) * 2
let screenHeight: Float = Float(map.frame.height) * 2
let minScreenPos: NTScreenPos = NTScreenPos(x: 0, y: 0)
let maxScreenPos: NTScreenPos = NTScreenPos(x: screenWidth, y: screenHeight)
let screenBounds = NTScreenBounds(min: minScreenPos, max: maxScreenPos)
let contain = screenBounds?.contains(map.map(toScreen: marker.getBounds().getCenter()))我意识到最好先要求这个职位,然后在NTScreenPos中转换那个NTScreenPos,然后问那个屏幕pos是否在实际的屏幕边界内。
在最后一个建议中,你说我需要乘以screenWidht和screenHeight的比例,所以我假设,如果我把UIScreen.main.scale和screenHeight相乘,这就是屏幕的比例?,因为控制台输出的地图宽度和高度是iphone屏幕的一半,所以我即兴地说:)使用UIScreen.main.scale会更好。
关于第三个建议,我会尝试并发回。
发布于 2017-11-28 09:55:15
好吧,这里有几件事.
,首先,,你什么时候做screenToMap计算?当您的mapView尚未完全呈现时,它将返回0(即使您的mapView已经有了一个框架)。
因此,您肯定不能在我们的viewDidLoad或viewWillAppear中这样做,但目前也不能在layoutSubviews之后执行。您需要在地图呈现之后计算它,这可以使用mapRenderer的onMapRendered事件来实现。
我们创建了一个与此相关的问题:https://github.com/CartoDB/mobile-sdk/issues/162
其次,如果您要求从CartoMobileSDK的方法中获得坐标,坐标已经返回到我们的内部坐标系中,这意味着您不需要进行任何额外的转换。要求界限和立场的正确方法是:
let minPosWGS = map.screen(toMap: minScreenPos)!
let maxPosWGS = map.screen(toMap: maxScreenPos)!以及:
let markerCenter = userMarker!.getBounds().getCenter()第三代,X在屏幕上和地图上从左到右增加,但是Y在屏幕上从上到下增加,但从下到顶在地图上增加e 224,因此您必须以这样的方式初始化min和max:
let screenWidth = Float(map.frame.size.width)
let screenHeight = Float(map.frame.size.height)
let minScreenPos = NTScreenPos(x: 0.0, y: screenHeight)
let maxScreenPos = NTScreenPos(x: screenWidth, y: 0)请注意,这个计算也取决于您的视图的方向和地图的旋转。目前,我们假设您的旋转为0,视图处于纵向模式。
,最后是,iOS使用缩放坐标,但是Carto的Mobile需要真正的坐标。所以你需要把你的价值按比例乘以:
let screenWidth = Float(map.frame.size.width * UIScreen.main.scale)
let screenHeight = Float(map.frame.size.height * UIScreen.main.scale)https://stackoverflow.com/questions/47476145
复制相似问题