【问题标题】:'Current location' BUTTON replica of the maps in iPhoneiPhone中地图的“当前位置”按钮副本
【发布时间】:2011-05-01 09:46:10
【问题描述】:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
    if(annotationView.annotation == mv.userLocation) {
        MKCoordinateRegion region;
        MKCoordinateSpan span;

        span.latitudeDelta=0.002;
        span.longitudeDelta=0.002; 

        CLLocationCoordinate2D location=mv.userLocation.coordinate;

        region.span=span;
        region.center=location;

        [mv setRegion:region animated:TRUE];
        [mv regionThatFits:region];
    }
}

大家好。我曾尝试搜索其他帖子和网站。本质上,我想复制“当前位置”按钮。

使用上面的代码,我可以放大用户当前的位置。然而,这并非完美无缺,有时 GPS 更新有点晚或认为我在其他地方,缩放到那个位置,但蓝点会从屏幕上移到我真正所在的位置。

我想知道有没有一种方法可以添加一个“按钮”,就像 iphone 上的地图一样。它不需要获得新的缩放等,只需移动到新的更新位置。我的主要代码源几乎可以复制到here

感谢您的帮助。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    使用相同的图形创建自定义注释。此对象必须实现MKAnnotationView,并且您必须在MKMapViewDelegate 的方法mapView:viewForAnnotation: 中为其返回自定义视图。如果您想要精确的图形,请使用项目UIKit-Artwork-Extractor 来获取它。

    保存对注释视图的引用,订阅核心位置更新,并更新位置调用如下:

    - (void) animateView:(UIView *)view toPosition:(NSValue*)value
    {
    CGPoint newCenter = [value CGPointValue];
    [UIView animateWithDuration:0.5
        animations:^{
            view.center = newCenter;
        }
        completion:^(BOOL finished){ }];
    }
    

    然后在注解上设置地图的中心:

    - (void)centerMap:(CLLocation *)location {
        MKCoordinateRegion region = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
        region.center = location.coordinate;
        region.span.longitudeDelta = 0.15f; 
        region.span.latitudeDelta = 0.15f;
        [self.mapView setRegion:region animated:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      相关资源
      最近更新 更多