Convert MKCoordinateRegion to MKMapRect

前端 未结 10 673
南笙
南笙 2020-12-02 10:52

I have a square MKMapView in my app, and I wish to set a center point and the exact height/width of the view in meters.

Creating an MKCoordinateRegion and setting th

10条回答
  •  旧巷少年郎
    2020-12-02 10:55

    Use MKMapPointForCoordinate to convert the 2 point of the region (top/left and bottom/right), then create the MKMapRect using the 2 MKMapPoints

            CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(latitude, longitude);
            CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(latitude + cellSize, longitude + cellSize);
    
            MKMapPoint upperLeft = MKMapPointForCoordinate(coordinateOrigin);
            MKMapPoint lowerRight = MKMapPointForCoordinate(coordinateMax);
    
            MKMapRect mapRect = MKMapRectMake(upperLeft.x,
                                              upperLeft.y,
                                              lowerRight.x - upperLeft.x,
                                              lowerRight.y - upperLeft.y);
    

提交回复
热议问题