MKCoordinateSpan in Meters?
Asked Answered
K

3

28

I need to create a MKCoordinateSpan that is about 500 meters.

How do I calculate the values to pass into the MKCoordinateSpan constructor?

Answers in any programming (Obj-C, .Net) language are fine.

Katy answered 11/9, 2011 at 22:41 Comment(0)
S
102

Another alternative is to use MapKit's MKCoordinateRegionMakeWithDistance function:

MKCoordinateRegion rgn = MKCoordinateRegionMakeWithDistance(
    CLLocationCoordinate2DMake(someLatitude, someLongitude), 500, 500);

The MKCoordinateSpan will be in rgn.span.

Sakhuja answered 12/9, 2011 at 2:49 Comment(0)
U
41

Unless you need great accuracy you can make it much easier with approximation. The first problem is to find the fraction of a degree of latitude representing 500 meters. Easy since a degree of latitude is a constant in any location, roughly 111 km. So 500 meters is .0045 degrees latitude.

Then it gets harder because length of a degree of longitude varies depending on where you are. It can be approximated with

enter image description here

where alpha is earth's equatorial radius, 6,378,137km, b/a is 0.99664719 (a constant in use for the WGC84 spheroid model in use by all GPS devices) and enter image description here where phi is the degree of latitude.

Imagine for a second you're lucky enough to be in Melbourne with a longitude of 37.783 degrees S. North or South doesn't matter here. beta works out to be 37.6899 and the rest of it solves to give a longitudinal degree a length of 88km. So 500 meters is .0057 of a degree.

Result for Melbourne - MKCoordinateSpan melbourne500MeterSpan = MKCoordinateSpanMake(.0045, .0057);

You can check your answers and your code with this online calculator

The wiki article on longitude has a lot more detail on this (and it the source of the images here)

Code:

#define EARTH_EQUATORIAL_RADIUS (6378137.0)
#define WGS84_CONSTANT (0.99664719)

#define degreesToRadians(x) (M_PI * (x) / 180.0)

// accepts decimal degrees. Convert from HMS first if that's what you have
double spanOfMetersAtDegreeLongitude(double degrees, double meters) {

    double tanDegrees = tanf(degreesToRadians(degrees));
    double beta =  tanDegrees * WGS84_CONSTANT;
    double lengthOfDegree = cos(atan(beta)) * EARTH_EQUATORIAL_RADIUS * M_PI / 180.0;
    double measuresInDegreeLength = lengthOfDegree / meters;
    return 1.0 / measuresInDegreeLength;
}
Uranyl answered 11/9, 2011 at 23:34 Comment(1)
Please don't use this answer. As gratifying as it is to get the points, MKCoordinateRegionMakeWithDistance is the right way.Uranyl
K
0

In MonoTouch, then using this solution you can use this helper method:

    public static void ZoomToCoordinateAndCenter (MKMapView mapView, CLLocationCoordinate2D coordinate, double meters, bool showUserLocationToo, bool animate)
    {
        if (!coordinate.IsValid ())
            return;

        mapView.SetCenterCoordinate (coordinate, animate);
        mapView.SetRegion (MKCoordinateRegion.FromDistance (coordinate, meters, meters), animate);      
    }
Katy answered 13/9, 2011 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.