How to find the radius of visible MKMapView visible screen area?

后端 未结 2 647
眼角桃花
眼角桃花 2021-01-21 04:13

I want to know the radius of visible area in iphone screen, as I will zoomout and zoom in the visible area will change, so I want to know the radius of that particular area, how

相关标签:
2条回答
  • 2021-01-21 04:39

    I might be misunderstanding the question, but isn't it as simple as:

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
        CGFloat latD = mapView.region.span.latitudeDelta;
        CGFloat lngD = mapView.region.span.longitudeDelta;
        NSLog(@"This is the latitude delta of the visible map: %f", latD);
        NSLog(@"This is the longitude delta of the visible map: %f", lngD);
    }
    
    0 讨论(0)
  • 2021-01-21 05:00

    Its not radius what is required.

    You need to use the region parameter from mapView.

    Check out apple docs, it is pretty much clear from those.

    Go thru this tutorial. It will help you a lot

    icode blog mapkit demo

    specifically you need to set something like this..

    MKCoordinateSpan span = [self coordinateSpanWithMapView:self centerCoordinate:centerCoordinate andZoomLevel:zoomLevel];
    MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);
    [self setRegion:region animated:animated];
    

    where span can be calculated as

    - (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView
                             centerCoordinate:(CLLocationCoordinate2D)centerCoordinate
                                 andZoomLevel:(NSUInteger)zoomLevel
    {
    // convert center coordiate to pixel space
    double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude];
    double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude];
    
    // determine the scale value from the zoom level
    NSInteger zoomExponent = 20 - zoomLevel;
    double zoomScale = pow(2, zoomExponent);
    
    // scale the map’s size in pixel space
    CGSize mapSizeInPixels = mapView.bounds.size;
    double scaledMapWidth = mapSizeInPixels.width * zoomScale;
    double scaledMapHeight = mapSizeInPixels.height * zoomScale;
    
    // figure out the position of the top-left pixel
    double topLeftPixelX = centerPixelX - (scaledMapWidth / 2);
    double topLeftPixelY = centerPixelY - (scaledMapHeight / 2);
    
    // find delta between left and right longitudes
    CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX];
    CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth];
    CLLocationDegrees longitudeDelta = maxLng - minLng;
    
    // find delta between top and bottom latitudes
    CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY];
    CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight];
    CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat);
    
    // create and return the lat/lng span
    MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
    return span;
    }
    

    Cheers :)

    0 讨论(0)
提交回复
热议问题