Getting the bounds of an MKMapView

前端 未结 10 1121
既然无缘
既然无缘 2020-11-28 20:28

In order to setup a query to an external server I want to get the bounds of the current Map View in an iPhone app I\'m building. UIView should respond to bounds but it seems

10条回答
  •  不知归路
    2020-11-28 20:47

    This code works with map rotating like 90/180 degrees. set mapView.pitchEnabled = NO; for less bugs.

    CLLocationDirection heading = mapView.camera.heading;
    
    float mapWidth = mapView.frame.size.width;
    float mapHeight = mapView.frame.size.height;
    
    float neX = mapWidth;
    float neY = 0.0;
    
    float swX = 0.0;
    float swY = mapHeight;
    
    
    if (heading >= 0 && heading <= 90) {
        //println("Q1")
        float ratio = heading / 90;
    
        neX = (1-ratio) * mapWidth;
        swX = (mapWidth*ratio);
    } else if (heading >= 90 && heading <= 180) {
        //println("Q2")
        float ratio = (heading - 90) / 90;
        neX = 0;
        neY = (mapHeight*ratio);
        swY = (1-ratio) * mapHeight;
        swX = mapWidth;
    
    } else if (heading >= 180 && heading <= 270) {
        //println("Q3")
        float ratio = (heading - 180) / 90;
        neX = mapWidth*ratio;
        neY = mapHeight;
        swX = (1-ratio) * mapWidth;
        swY = 0;
    
    } else if (heading >= 270 && heading <= 360) {
        //println("Q4");
        float ratio = (heading - 270) / 90;
        neX = mapWidth;
        neY = (1-ratio) * mapHeight;
        swY = ratio * mapHeight;
    
    }
    
    CGPoint swPoint = CGPointMake(swX, swY);
    CGPoint nePoint = CGPointMake(neX, neY);
    
    CLLocationCoordinate2D swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
    CLLocationCoordinate2D neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
    

提交回复
热议问题