【问题标题】:How to center my current location in MKMapView?如何在 MKMapView 中将我的当前位置居中?
【发布时间】:2012-01-05 14:07:21
【问题描述】:

我通过使用showUserLocation 启用在MKMapView 中显示current location。我还想将mapview 居中到用户当前位置以及位置的缩放地图。请帮助我解决这个问题,因为我没有从 stackoverflow 上的其他类似问题中获得任何帮助。

我的代码如下:

- (void)viewDidLoad {
  [super viewDidLoad];
  [mapView setMapType:MKMapTypeStandard];
  [mapView setZoomEnabled:YES];
  [mapView setScrollEnabled:YES];
  [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
  [mapView setDelegate:self];
}

【问题讨论】:

    标签: iphone ios mkmapview


    【解决方案1】:

    要以用户位置为中心,可以使用以下代码:

    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
    

    要放大特殊位置,您应该研究区域 (MKCoordinateRegion) 的计数和工作方式,计算区域的值并使用调用显示它:

    [mapView setRegion:myRegion animated:YES];
    

    这个sample WorldCities 展示了区域显示的基础知识。

    【讨论】:

    • 我也在做同样的事情,但它不起作用......如果可能的话还有其他建议......提前谢谢......
    • 也许你可以在你的问题中发布你的代码,这样就很容易猜出问题了=)
    • 我已经更新了有问题的代码,这不会使 mapView 居中到 currentLocation。请检查并帮助我....提前致谢。
    • 如果你试图从 viewDidLoad 设置它:它会失败,因为地图视图还不知道用户位置(它需要一些时间来接收来自 GPS 的响应);所以从 UIMapViewDelegate 方法调用 [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES] 是正确的 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    • 如何在用户位置添加放大功能?
    【解决方案2】:
    MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta = 0.2;     // 0.0 is min value u van provide for zooming
        span.longitudeDelta= 0.2;
    
        CLLocationCoordinate2D location = [self addressLocation];
        region.span=span;
        region.center =location;     // to locate to the center
        if(addAnnotation != nil) {
            [mapView removeAnnotation:addAnnotation];
            [addAnnotation release];
            addAnnotation = nil;
        }
        addAnnotation = [[AddressANnotation alloc] initWithCoordinate:location];
        [mapView addAnnotation:addAnnotation];
        [mapView setRegion:region animated:TRUE];
        [mapView regionThatFits:region];
    

    这有助于我在地图视图的中心显示位置。

    【讨论】:

      【解决方案3】:

      对于 MKMapView

      self.mapView.showsUserLocation = YES;
      self.mapView.userTrackingMode = MKUserTrackingModeFollow;
      

      【讨论】:

        【解决方案4】:
        -(void) removeZoom:(float)deltaValue
        {
            MKCoordinateRegion region;
            region.center.latitude =  self.locationManager.location.coordinate.latitude;
            region.center.longitude = self.locationManager.location.coordinate.longitude;
        
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];
            region.span.latitudeDelta = deltaValue;
            region.span.longitudeDelta = deltaValue;
            region = [mapViewSelf regionThatFits:region];
            [mapViewSelf setRegion:region animated:YES];
            [UIView commitAnimations];
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-21
          • 2014-10-16
          • 1970-01-01
          • 2012-09-16
          • 1970-01-01
          相关资源
          最近更新 更多