【问题标题】:Adding random MKAnnotations around Current Location in a MKMapView在 MKMapView 中的当前位置周围添加随机 MKAnnotations
【发布时间】:2012-05-22 03:22:32
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: ios cocoa-touch mkmapview mapkit mkannotation


    【解决方案1】:

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

    查看下面的代码

    #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 公里范围的数组。 使用 annotations 数组并将您的注释显示在带有用户位置的地图视图中。

    【讨论】:

      【解决方案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
      

      【讨论】:

        【解决方案3】:

        在 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)
        }
        

        【讨论】:

          【解决方案4】:

          迅速 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
          }
          

          【讨论】:

          • 请添加描述。什么是 nw,se。
          • @NikKov 描述添加
          猜你喜欢
          • 1970-01-01
          • 2011-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-16
          • 1970-01-01
          • 2014-10-16
          相关资源
          最近更新 更多