在某个区域中创建随机CLLocationCoordinate

kad*_*ian 0 random objective-c map ios

我有一个MKMapRect.

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

我知道有arc4random(),但我怎样才能将它用于GPS坐标?

Mar*_*tel 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);
Run Code Online (Sandbox Code Playgroud)

  • 如上所述,这将仅在地图rect中沿对角线生成点,因为您对每个轴使用相同的随机值.(即你想使用不同的`val`来制作randomPoint.y) (2认同)