【问题标题】:MKMapView - Zoom to fit nearest annotations around userlocationMKMapView - 缩放以适应用户位置周围最近的注释
【发布时间】:2014-02-06 15:19:44
【问题描述】:

我想缩放我的地图视图以显示最近注释中的至少一个注释,以尽可能高的缩放比例和用户位置。我尝试了以下方法:

-(void)zoomToFitNearestAnnotationsAroundUserLocation {


    MKMapPoint userLocationPoint = MKMapPointForCoordinate(self.restaurantsMap.userLocation.coordinate);
    MKCoordinateRegion region;
    if ([self.restaurantsMap.annotations count] > 1) {

        for (id<MKAnnotation> annotation in self.restaurantsMap.annotations) {

          MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
          CLLocationDistance distanceBetweenAnnotationsAndUserLocation = MKMetersBetweenMapPoints(annotationPoint, userLocationPoint);


             region =  MKCoordinateRegionMakeWithDistance(self.restaurantsMap.userLocation.coordinate, distanceBetweenAnnotationsAndUserLocation, distanceBetweenAnnotationsAndUserLocation);


        }

        [self.restaurantsMap setRegion:region animated:YES];

    }



}

我将如何设法保存 2-3 个最近距离并根据该信息创建一个区域?

【问题讨论】:

    标签: ios iphone mkmapview


    【解决方案1】:

    如果您正在为 iOS 7 及更高版本进行开发,您可以保存一组按用户位置与注释之间的距离排序的注释,然后获取前 3 个注释。一旦你有了这些,你可以使用showAnnotations:animated: 来定位地图,以便所有注释都可见。

    这是另一种方式(取自here):

    MKMapRect zoomRect = MKMapRectNull;
    for (id <MKAnnotation> annotation in mapView.annotations)
    {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
        zoomRect = MKMapRectUnion(zoomRect, pointRect);
    }
    [mapView setVisibleMapRect:zoomRect animated:YES];
    
    //You could also update this to include the userLocation pin by replacing the first line with
    MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
    MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
    

    当然,您必须更新第二个解决方案以仅使用最近的注释点,但您已经知道如何找到它们,所以这不成问题。

    【讨论】:

      【解决方案2】:

      将注释存储在数组中

      添加此功能并根据需要更改距离

      - (MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations
      {
      MKCoordinateRegion region;
      
      if ([annotations count] == 0)
      {
          region = MKCoordinateRegionMakeWithDistance(_mapView.userLocation.coordinate, 1000, 1000);
      }
      
      else if ([annotations count] == 1)
      {
          id <MKAnnotation> annotation = [annotations lastObject];
          region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);
      } else {
          CLLocationCoordinate2D topLeftCoord;
          topLeftCoord.latitude = -90;
          topLeftCoord.longitude = 180;
      
          CLLocationCoordinate2D bottomRightCoord;
          bottomRightCoord.latitude = 90;
          bottomRightCoord.longitude = -180;
      
          for (id <MKAnnotation> annotation in annotations)
          {
              topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
              topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
              bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
              bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
          }
      
          const double extraSpace = 1.12;
          region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) / 2.0;
          region.center.longitude = topLeftCoord.longitude - (topLeftCoord.longitude - bottomRightCoord.longitude) / 2.0;
          region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * extraSpace;
          region.span.longitudeDelta = fabs(topLeftCoord.longitude - bottomRightCoord.longitude) * extraSpace;
      }
      
      return [self.mapView regionThatFits:region];
      }
      

      然后这样称呼它

       MKCoordinateRegion region = [self regionForAnnotations:_locations];
      [self.mapView setRegion:region animated:YES];
      

      现在缩放应该适合数组中的所有注释

      祝你好运!!

      【讨论】:

        猜你喜欢
        • 2011-06-08
        • 2015-07-08
        • 2016-08-22
        • 1970-01-01
        • 1970-01-01
        • 2011-08-11
        • 2011-08-29
        • 1970-01-01
        • 2011-02-22
        相关资源
        最近更新 更多