ios - map 注释的自定义标题

标签 ios mapkit

在我的 map 应用中,用户可以在搜索栏中输入地址,该位置会以红色图钉显示。在我的代码中是否可以让用户在将图钉放置在 map 上之前输入自定义标题和副标题?标题和副标题应显示在标注气泡中。这是怎么做到的?现在,我所有的图钉标题都是“我的位置”。

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    if (!self.geocoder)
    {
        self.geocoder = [[CLGeocoder alloc] init];
    }

    NSString *address = [NSString stringWithFormat:@"%@", self.searchBar.text];

    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0)
        {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            CLLocation *location = placemark.location;
            CLLocationCoordinate2D coordinate = location.coordinate;

            NSLog (@"%f %f", coordinate.latitude, coordinate.longitude);

            MKCoordinateRegion region;
            MKCoordinateSpan span;
            span.latitudeDelta = 0.01;
            span.longitudeDelta = 0.01;
            region.span = span;
            region.center = coordinate;

            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            [annotation setCoordinate:coordinate];
            [annotation setTitle:@"My Place"];
            [[self mapView] addAnnotation:annotation];

            [self.mapView setRegion:region animated:TRUE];
            [self.mapView regionThatFits:region];

            [self.searchBar resignFirstResponder];
            self.searchBar.text = @"";

        }
    }];

}

另外,如何将我的所有 pin 保存到 NSUserDefault,以便在应用程序重新启动时显示它们?这行得通吗,还是只能节省一个引脚?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setDouble:coordinate.latitude forKey:allPins_latitude];
                [defaults setDouble:coordinate.longitude forKey:allPins_longitude];
                [defaults setBool:YES forKey:allPins_coordinates];
                [defaults synchronize];

如果有任何帮助,我将不胜感激!谢谢!

最佳答案

当然,您可以让用户输入标题。

上面的代码创建一个注释对象并将其标题设置为固定标题。

编辑:

请注意,此答案来自 2013 年。UIAlertView 此后已被弃用。在最新版本的 iOS 中,您应该使用 UIAlertController。如果我有时间,我会提供有关使用 UIAlertController 细节的最新指南,但它有很好的文档记录,应该有很多示例可供查看。


不是这样做,而是创建您的注释,将其保存到实例变量,并使用 UIAlertViewStylePlainTextInput 显示警报 View 以收集注释的名称。使自己成为警报 View 的委托(delegate),然后在您的 alertView:clickedButtonAtIndex: 方法中,如果用户单击确定,则从警报 View 获取标题,在注释上设置标题属性,并将注释添加到 map

至于将注释保存为用户默认值:

我建议创建您自己的符合 MKAnnotation 对象的对象。它只需要有一个坐标属性、一个标题属性和一个副标题属性。让你的注解对象符合NSCoding协议(protocol)(实现initWithCoder和encodeWithCoder。)

完成此操作后,您可以使用 NSKeyedArchiver 类方法 archivedDataWithRootObject 将整个注释对象数组转换为 NSData。然后只需将数据保存为用户默认值即可。启动时,读取 NSData 对象,使用 unarchiveObjectWithData: 将其转换回自定义注释对象数组,并将注释添加回 map 。

关于ios - map 注释的自定义标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18689709/

相关文章:

iphone - MKMapView 缩放和区域

ios - React Native - iOS - 无效的 `Podfile` 文件 : undefined method `[]' for nil:NilClass

ios - 使用 swift 和 parse 保持当前用户登录

swift - 如何使用 Swift 2 以编程方式从按钮打开 View Controller ?

objective-c - 删除mapkit中的边框

ios - Swift 2 MapKit Annotations - 如何对注释进行分组?

IOS-垂直向上移动多个图像

ios - Apple 拒绝了应用程序,因为它在未经用户许可的情况下传输 MAC 地址

ios - 在 swift ios 10 上使用 Facebook 登录时浏览器页面空白

iOS 10 MapKit 上层缩放问题