我正在试图找出MKMapRect的大小(即iPhone的320x568点)。
是否有类似于将协调点转换为点的东西?即
[self.mapView convertCoordinate:coordinate1 toPointToView:self.view];发布于 2014-02-27 02:18:55
映射视图具有convertRegion:toRectToView:方法,该方法接受MKCoordinateRegion并将其转换为相对于指定视图的CGRect。
如果您有一个MKMapRect,首先使用MKCoordinateRegionForMapRect函数将其转换为MKCoordinateRegion,然后调用convertRegion:toRectToView:。
示例:
MKCoordinateRegion mkcr = MKCoordinateRegionForMapRect(someMKMapRect);
CGRect cgr = [mapView convertRegion:mkcr toRectToView:self.view];请记住,虽然某些固定区域的MKMapRect不会随着地图的缩放或平移而改变,但对应的CGRect将在其origin和size中发生变化。
发布于 2017-01-25 10:58:39
也许作为一个实际例子..。我使用这段代码向屏幕上的地图添加一个覆盖层,然后检查屏幕的哪个部分是否需要更新。
此方法是MKOverlay类的一部分。我的UIViewController名为"MyWaysViewController“,屏幕上的映射名为"MapOnScreen”(只是为了理解代码)。
它的Swift 3/ IOS 10代码
/**
-----------------------------------------------------------------------------------------------
adds the overlay to the map and sets "setNeedsDisplay()" for the visible part of the overlay
-----------------------------------------------------------------------------------------------
- Parameters:
- Returns: nothing
*/
func switchOverlayON() {
DispatchQueue.main.async(execute: {
// add the new overlay
// if the ViewController is already initialised
if MyWaysViewController != nil {
// add the overlay
MyWaysViewController!.MapOnScreen.add(self)
// as we are good citizens on that device, we check if and for what region we setNeedsDisplay()
// get the intersection of the overlay and the visible region of the map
let visibleRectOfOverlayMK = MKMapRectIntersection(
self.boundingMapRect,
MyWaysViewController!.MapOnScreen.visibleMapRect
)
// check if it is null (no intersection -> not visible at the moment)
if MKMapRectIsNull(visibleRectOfOverlayMK) == false {
// It is not null, so at least parts are visible, now a two steps aproach to
// convert MKMapRect to cgRect. first step: get a coordinate region
let visibleRectCoordinateRegion = MKCoordinateRegionForMapRect(visibleRectOfOverlayMK)
// second step, convert the region to a cgRect
let visibleRectOfOverlayCG = MyWaysViewController!.MapOnScreen.convertRegion(visibleRectCoordinateRegion, toRectTo: MyWaysViewController!.MapOnScreen)
// ask to refresh that cgRect
MyWaysViewController!.MapOnScreen.setNeedsDisplay(visibleRectOfOverlayCG)
}
}
})
}https://stackoverflow.com/questions/22053333
复制相似问题