开发者

MKCoordinateSpan in Meters?

I need to create a MKCoord开发者_运维知识库inateSpan 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.


Another alternative is to use MapKit's MKCoordinateRegionMakeWithDistance function:

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

The MKCoordinateSpan will be in rgn.span.


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

MKCoordinateSpan in Meters?

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

MKCoordinateSpan in Meters?

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;
}


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);      
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜