iosswift3mapscarto-mobile

How to verify that a Map Position is inside the Map Bounds using carto-mobile SDK


what i am trying to achieve is this, when the userMarker be inside the visible bounds accomplish some actions, for that this is my code.

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)

But always the output is false, what i am doing wrong, any help, please.


Solution

  • Hi @Nikitah i end up with this solution

    i implement onMapMoved event from MapEventsListener an there i ask for this

    if latestLocation != nil {
        delegate?.hideLocationButton()
    }
    

    So in mi hideLocationButton method i do this

    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()))
    

    I realise that it was better ask for the position and then convert that NTMapPos in NTScreenPos, and ask if that screen pos was inside the the actual screen bounds.

    In the finally suggestion you said that i need to multiply by the scale so i supouse that where i multiply the screenWidht and screenHeight by two this will be the screen scale?, i did that bacause the map width&height in console outpout was the half of the iphone screen so i improvise :), will be better to use UIScreen.main.scale

    About the third suggestion i will try it and post back.