2017-11-24 4 views
1

私が達成しようとしているのは、userMarkerが可視範囲内にあるときに、これが自分のコードであるということです。マップ位置が地図の中にあることを確認する方法carto-mobile SDK

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) 

しかし、出力は常にfalseです。間違っていると助けてください。

答えて

0

こんにちは@Nikitah私はMapEventsListenerそこから私はこの

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

をお願いしonMapMovedイベントを実装し、このソリューション

で終わりますin 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でそのNTMapPosを変換し、そのスクリーンの位置が実際のスクリーン境界の内側にあるかどうかを尋ねる方がいいでしょう。

最後に示唆したことは、私はスケールを乗算する必要があると言ったので、スクリーンWidhtとscreenHeightを乗算するとスクリーンの倍率になるでしょうか?私は地図の幅を小さくします&コンソールの高さoutpoutはiphone画面の半分だったので、私は即興:)、UIScreen.main.scaleを使う方が良いでしょう。

私はそれを試してポストバックします。

+0

あなたのソリューションがあなたのためにうまくいくなら、しかし、ええ、あなたはまだそれがどのように異なる方向と回転で動作するかを調べるべきです。 'UIScreen.main.scale'を使うべきです。あなたの現在のデバイス/シミュレータのスケールは2であるかもしれませんが、それはすべてのデバイスに当てはまりません。 – Nikitah

+0

こんにちは、私はすべての回転と向きでそれをテストし、私は、 'UIScreen.main.scale' –

1

さてさて、いくつかのものがここにあります....

まず、あなたのscreenToMap計算をするときにやっているの? mapViewが完全にレンダリングされていない場合(mapViewに既にフレームがある場合でも)、0が返されます。

viewDidLoadまたはviewWillAppearでは間違いなくそれを行うことはできませんが、現時点ではlayoutSubviews以降ではありません。マップがレンダリングされた後に計算する必要があります。これはmapRendereronMapRenderedイベントを使用して実現できます。ここで利用可能

例:https://github.com/CartoDB/mobile-ios-samples/blob/master/AdvancedMap.Objective-C/AdvancedMap/CaptureController.mm

私たちは、このに関する問題を作成しました: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を上から下へあなたの画面では下から上へあなたのマップではminとmaxを初期化する必要がありますch:

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のモバイルSDKでは真の座標が必要です。だから、規模によって自分の価値観を乗算する必要があります。

let screenWidth = Float(map.frame.size.width * UIScreen.main.scale) 
let screenHeight = Float(map.frame.size.height * UIScreen.main.scale) 
+0

あなたの提案のおかげで私は私があなたのポストの前に見つけた答えを投稿してください、それを見て、あなたの提案を教えてください:) –

関連する問題