0

我有一个MKMapRect.

我如何CLLocationCoordinate在里面创建一个随机的?

我知道有arc4random(),但是如何将它用于 GPS 坐标?

4

3 回答 3

5
#define ARC4RANDOM_MAX      0x100000000
... 

//val is a double between 0 and 1
double xOffset = ((double)arc4random() / ARC4RANDOM_MAX);
double yOffset = ((double)arc4random() / ARC4RANDOM_MAX);
MKMapPoint randomPoint;

randomPoint.x = maprect.origin.x + xOffset*maprect.size.width;
randomPoint.y = maprect.origin.y + yOffset*maprect.size.height;

CLLocationCoordinate2D randomCoordinate = MKCoordinateForMapPoint(randomPoint);
于 2013-05-12T14:53:38.707 回答
2

从 MapRect 中,您可以得到MKMapPoint originand MKMapSize size,那么随机 CLLocationCoordinate 应该是{origin.x + [0 ~ size.width], origin.y + [0 ~ size.height]}

typedef struct {
       MKMapPoint origin;
       MKMapSize size;
    } MKMapRect;

像这样的代码:

#define ARC4RANDOM_MAX      0x100000000
- (double)createRandomsizeValueFloat:(double)fromFloat toFloat:(double)toFloat
{
    if (toFloat < fromFloat) {
        return toFloat;
    } else if (toFloat == fromFloat) {
        return fromFloat;
    }
    return ((double)arc4random() / ARC4RANDOM_MAX) * (toFloat - fromFloat) + fromFloat;
}

//CLLocationDegrees lat = mapRect.origin.x + [self createRandomsizeValueFloat:0 toFloat:mapRect.size.width];
//CLLocationDegrees lng = mapRect.origin.y + [self createRandomsizeValueFloat:0 toFloat:mapRect.size.height];
于 2013-05-12T14:47:12.013 回答
0

只需设置实例的latitudelongitude属性。CLLocationCoordinate

注意范围:-90 < 纬度 < 90,-180 < 经度 < 180。

希望这可以帮助。

于 2013-05-12T14:47:31.260 回答