3

我有一个MKMapView显示当前用户位置的。当我单击导航栏中的按钮时,我想随机放置 10 个MKAnnotation图钉。这些可以随机放置在任何地方,但只能在当前可见的地图区域内和当前位置周围。

怎么会去做这样的事情?有没有办法在用户位置附近但在地图区域内有一个长/纬度范围?那么,我可以从这个范围中随机选择吗?

基本上,我需要找到当前可用的坐标MKCoordinateRegion

有没有更好的方法来解决这个问题?

4

4 回答 4

4

您可以使用用户位置与其他位置之间的距离来获取注释

看看下面的代码

#define DISTANCE_RADIUS     10.0    // in KM
-(NSArray *)findNearMe:(NSArray *)lounges {
NSMutableArray *arNearme = [NSMutableArray array];

CLLocation *currentLocation;
for(NSDictionary *d in lounges) 
{

currentLocation = [[CLLocation alloc] initWithLatitude:29.33891 longitude:48.077202];

    CGFloat latitude=[[d valueForKey:@"latitude"] floatValue];
    CGFloat longitude=[[d valueForKey:@"longitude"] floatValue];
    CLLocation *newPinLocation=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    double distanceValue=[currentLocation distanceFromLocation:newPinLocation]; 

    if(distanceValue/1000.0<=DISTANCE_RADIUS) {
        [arNearme addObject:d];
    }
}
return  arNearme;
}

它将返回用户位置附近 1 到 1000 公里范围的数组。使用注释数组并将您的注释显示在带有用户位置的地图视图中。

于 2012-05-22T04:42:39.637 回答
2

我想通了,我是这样做的:

/**
 * Adds new pins to the map
 *
 * @version $Revision: 0.1
 */
+ (void)addPinsToMap:(MKMapView *)mapView amount:(int)howMany {

    //First we need to calculate the corners of the map so we get the points
    CGPoint nePoint = CGPointMake(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
    CGPoint swPoint = CGPointMake((mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

    //Then transform those point into lat,lng values
    CLLocationCoordinate2D neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
    CLLocationCoordinate2D swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];

    // Loop
    for (int y = 0; y < howMany; y++) {
        double latRange = [MapUtility randomFloatBetween:neCoord.latitude andBig:swCoord.latitude];
        double longRange = [MapUtility randomFloatBetween:neCoord.longitude andBig:swCoord.longitude];

        // Add new waypoint to map
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latRange, longRange);
        MPin *pin = [[MPin alloc] init];
        pin.coordinate = location;
        [mapView addAnnotation:pin];

    }//end

}//end


/**
 * Random numbers
 *
 * @version $Revision: 0.1
 */
+ (double)randomFloatBetween:(double)smallNumber andBig:(double)bigNumber {
    double diff = bigNumber - smallNumber;
    return (((double) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}//end
于 2012-05-22T04:34:36.113 回答
0

在 Swift 3.0 中

func addPins() {
    let nePoint = CGPoint(x: mapView.bounds.origin.x + mapView.bounds.size.width, y: mapView.bounds.origin.y)
    let sePoint = CGPoint(x: mapView.bounds.origin.x, y: mapView.bounds.origin.y + mapView.bounds.size.height)

    let neCoord = mapView.convert(nePoint, toCoordinateFrom: mapView)
    let seCoord = mapView.convert(sePoint, toCoordinateFrom: mapView)

    var y = 5
    while y > 0 {
        let latRange = randomBetweenNumbers(firstNum: Float(neCoord.latitude), secondNum: Float(seCoord.latitude))
        let longRange = randomBetweenNumbers(firstNum: Float(neCoord.longitude), secondNum: Float(seCoord.longitude))
        let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(latRange), longitude: CLLocationDegrees(longRange))
        let pin = MKPointAnnotation()
        pin.coordinate = location
        pin.title = "Home"
        mapView.addAnnotation(pin)
        y -= 1
    }

}

func randomBetweenNumbers(firstNum: Float, secondNum: Float) -> Float{
    return Float(arc4random()) / Float(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
于 2016-12-01T15:40:28.497 回答
0

迅捷4:

// returned coordinates all restricted to the provided bound
// nwCoordinate: north West coordinate of the target bound
// seCoordinate: south east coordinate of the target bound

func createRandomLocation(nwCoordinate: CLLocationCoordinate2D, seCoordinate: CLLocationCoordinate2D) -> [CLLocationCoordinate2D]
{
   return (0 ... 30).enumerated().map { _ in
        let latitude = randomFloatBetween(nwCoordinate.latitude, andBig: seCoordinate.latitude)
        let longitude = randomFloatBetween(nwCoordinate.longitude, andBig: seCoordinate.longitude)
        return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }
}

private func randomFloatBetween(_ smallNumber: Double, andBig bigNumber: Double) -> Double {
    let diff: Double = bigNumber - smallNumber
    return ((Double(arc4random() % (UInt32(RAND_MAX) + 1)) / Double(RAND_MAX)) * diff) + smallNumber
}
于 2018-04-14T14:09:01.907 回答