Convert span value into meters on a mapview

前端 未结 6 813
小鲜肉
小鲜肉 2020-12-13 06:56

Whenever the user zoom in or out the map i need to know how many meters are currently represented on the map (width or height).

What i need is the inverse function o

相关标签:
6条回答
  • 2020-12-13 07:31

    Actually it was a really stupid mistake, if i do the same operation along the X axis then i get the correct value :

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    MKMapRect mRect = self.map.visibleMapRect;
    MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
    MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
    
    self.currentDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
    }
    
    0 讨论(0)
  • 2020-12-13 07:41

    Swift 4

    extension MKMapView {
      func regionInMeter() -> CLLocationDistance {
        let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))
        let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))
    
        return MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
      }
    }
    
    0 讨论(0)
  • 2020-12-13 07:45

    Here's an easier way (to get width and height in meters)...

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    
        MKMapRect rect = mapView.visibleMapRect;
    
        double mapWidth = MKMapRectGetWidth(rect) / 10;
        double mapHeight = MKMapRectGetHeight(rect) / 10;
    
    }
    
    0 讨论(0)
  • 2020-12-13 07:47

    Thanks for the post all. I have an app that required a mile radius to figure out how many location records to fetch so this came in handy. Here is the swift equivalent for anyone who might come across this in the future.

    let mRect: MKMapRect = self.mapView.visibleMapRect
            let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect))
            let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect))
            let currentDistWideInMeters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
            let milesWide = currentDistWideInMeters / 1609.34  // number of meters in a mile
            println(milesWide)
    
    0 讨论(0)
  • 2020-12-13 07:52
    - (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated {
      MKCoordinateSpan span = mapView.region.span;
      NSLog(@" 1 = ~111 km -> %f = ~ %f km ",span.latitudeDelta,span.latitudeDelta*111);
    }
    

    According to the docs http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html,

    latitudeDelta

    The amount of north-to-south distance (measured in degrees) to display on the map. Unlike longitudinal distances, which vary based on the latitude, one degree of latitude is always approximately 111 kilometers (69 miles).

    0 讨论(0)
  • 2020-12-13 07:53

    Swift 4.2

        func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    
            let mapRect = mapView.visibleMapRect
            let westMapPoint = MKMapPoint(x: mapRect.minX, y: mapRect.midY)
            let eastMapPoint = MKMapPoint(x: mapRect.maxX, y: mapRect.midY)
            let visibleDistance = westMapPoint.distance(to: eastMapPoint)
        }
    
    0 讨论(0)
提交回复
热议问题