在MKMapView中的当前位置周围添加随机MKAnnotations

时间:2012-05-22 03:22:32

标签: ios cocoa-touch mkmapview mapkit mkannotation

我有一个显示当前用户位置的MKMapView。当我单击导航栏中的按钮时,我想要随机删除10个MKAnnotation个引脚。这些可以随机丢弃,但仅限于当前可见地图区域和当前位置。

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

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

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

4 个答案:

答案 0 :(得分: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 km范围的数组。 使用注释数组并在用户位置的地图视图中显示注释。

答案 1 :(得分: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

答案 2 :(得分: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)
}

答案 3 :(得分: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
}