【问题标题】:iPhone - remove pin annotations of map, but not mineiPhone - 删除地图的引脚注释,但不是我的
【发布时间】:2023-03-15 20:10:02
【问题描述】:

我使用 MapView 并在其上设置注释(紫色大头针)和我的用户位置(蓝色圆圈)。 因为紫色的大头针注释会移动,所以我必须删除它们并将它们设置为新的地图。

我设置为:

CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;
NSUInteger count = 1;
for(int i = 0; i < 10; i++) {
    CGFloat latDelta = rand()*.035/RAND_MAX - .02;
    CGFloat longDelta = rand()*.03/RAND_MAX - .015;
    CLLocationCoordinate2D newCoord = {coordinate.latitude+latDelta, coordinate.longitude+longDelta};
    MyMapAnnotation* annotation = [[MyMapAnnotation alloc] initWithCoordinate:newCoord andID:count++];
    [mapView addAnnotation:annotation];
    [annotation release];
}

在那之前,我做了一个

[mapView removeAnnotations:mapView.annotations];

但是这条线也用蓝点删除了我的位置! 如果不删除我的位置,我怎么能做到这一点。

非常感谢提前和最好的问候。

【问题讨论】:

    标签: iphone mkmapview mkpinannotationview


    【解决方案1】:

    一个非常简单的方法是在删除注释的循环之前包含这一行:

        self.mapView.showsUserLocation = NO;
    

    然后在循环之后,将用户位置放回

        self.mapView.showsUserLocation = YES;
    

    编辑:我刚刚看到你没有遍历这些位置来删除它们。我不确定这是否适用于您的操作方式。

    【讨论】:

    • 嗯,我怎样才能让它变得更好?因为我这样做: - (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id )annotation { if (annotation == mapViewLocal.userLocation) { mapViewLocal.userLocation.title = @"Test"; mapViewLocal.userLocation.subtitle = @"Test Sub"; [mapViewLocal setRegion:MKCoordinateRegionMakeWithDistance(mapViewLocal.userLocation.coordinate, 3000, 3000) 动画:NO];返回零; } ...所以我将始终将我的位置设置在中心。并且没有出现蓝点。我必须移动 MapView,然后才会显示蓝点。
    • 要获得带有注释的地图视图以居中,遍历它们并找到最大和最小纬度和经度,然后使用这些设置区域跨度和中心。如果您想在注释中包含用户位置,请确保使用我上面编写的方法显示它。 MKMapView 有一个名为 annotations 的属性,它存储所有注释(包括用户位置)。如果您只想将地图以用户位置为中心,请使用setCenterCoordinate:animated:
    • 如果你能花 5 美元,这个教程真的帮助我理解了地图视图:pragprog.com/screencasts/v-bdmapkit/using-map-kit
    • 我重新组织了我的代码。我在循环中创建注释对象并将其添加到 NSMutableArray。我使用了remove-annotations-method,哪个参数是这个NSMuatable-Annotation-Array。效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2016-08-15
    • 2011-08-14
    相关资源
    最近更新 更多