【问题标题】:mapview blue dot not coming for second timemapview蓝点第二次没有出现
【发布时间】:2011-07-19 10:52:53
【问题描述】:

我正在开发一个应用程序来显示特定邮政编码中可用的商店。我从当前位置开始。我的当前位置出现蓝色脉动点。当我输入邮政编码时,我可以在地图视图中显示注释。我有一个“当前位置”按钮,可以再次返回当前位置。但是第二次我没有得到蓝点?我怎么得到它,这是代码:

- (void)showCurrentLocation 
{  
 self.mapView.showsUserLocation = YES;
    GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:self.mapView.userLocation.coordinate];

    annotation.title = @"Location";
    [self.mapView addAnnotation:annotation];
    [annotation release];    

    MKCoordinateRegion zoomIn;
    zoomIn.center.latitude = self.mapView.userLocation.coordinate.latitude;
    zoomIn.center.longitude=self.mapView.userLocation.coordinate.longitude;

    zoomIn.span.latitudeDelta = .1f;
    zoomIn.span.longitudeDelta =.1f;

    self.mapView.showsUserLocation = YES;
    [self.mapView setRegion:zoomIn animated:TRUE];
}

// annotation view 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *)annotation
{
    if ([[annotation title] isEqualToString:@"Location"]) 
    {
        MKAnnotationView *annotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];

        annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
        annotationView.annotation = annotation;
        return  annotationView;
    }

    else  if ([annotation isKindOfClass:[MKUserLocation class]])
    {        
        return nil;
    }
}

【问题讨论】:

  • 为什么要将 GPSAnnotation 坐标设置为与用户位置相同? viewForAnnotation 方法必须给你编译器警告“控制到达非无效函数的结尾”。至少在方法的末尾添加return nil; 来解决这个问题。除此之外还有其他一些问题。
  • 嗨安娜卡列尼娜,感谢您的回复。我尝试添加“return nil”,即使这样我第二个 tym 也没有得到 tat blue dot。

标签: iphone ipad ios4 mkmapview mkmapviewdelegate


【解决方案1】:

您是否在某个地方创建了 [self.mapView removeAnnotations:self.mapView.annotations] 来清空地图?这是一个常见错误,因为您还通过这样做删除了用户位置。

【讨论】:

  • 你好银口,感谢您的回复。是的,我正在删除它。但是又不是一种向用户当前位置添加注释的方法吗??
  • 可以,可以通过[mapView addAnnotation:mapView.userlocation]重新添加;但最好还是不要删除它,因为每次将它重新添加到地图时,它都会动画,这可能会导致 UI 滞后。如果你这样做会更好:NSMutableArray *annotationsToRemove = [[NSMutableArry alloc] initWithArray: mapView.annotations]; [annotationsToRemove removeAnnotation: mapView.userLocation]; [mapView removeAnnotations: annotationsToRemove]; 这将从地图中删除除 userLocation 之外的所有注释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 2021-09-24
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多