地图注释的自定义标题

时间:2013-09-08 23:33:24

标签: ios mapkit

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

- (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 = @"";

        }
    }];

}

此外,我的所有引脚如何保存到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];

我将不胜感激任何帮助!谢谢!

3 个答案:

答案 0 :(得分:1)

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

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

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

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

我建议创建一个符合MKAnnotation对象的自己的对象。它只需要一个坐标属性,一个title属性和一个subtitle属性。使您的注释对象符合NSCoding协议(实现initWithCoder和encodeWithCoder。)

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

答案 1 :(得分:0)

如果要为每个点设置不同的标题和副标题,则需要提供一个文本框供用户输入数据。完成后,它就像您已经拥有的代码一样简单

NSString *title = [self.titleField stringValue];
[annotation setTitle:@"title"];
NSString *subtitle = [self.subtitleField stringValue];
[annotation setSubtitle:@"title"];

但是,您可能没有屏幕空间来显示三个文本字段。 I do创建带有默认名称的图钉,然后允许用户打开标注气泡,或者自动打开它并显示一个按钮进行编辑,然后在顶部显示模态对话框以编辑标题和字幕

如果您在密钥coordinate.latitude下保存了每个allPins_latitude,那么每个{@}}都会覆盖之前的密码,而您只保存最后一个密码。如果您的引脚都存储为数组,请存储该数组。

答案 2 :(得分:0)

@petter我认为下面的代码会对你有帮助  // swift 3

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

       let Userlocation : CLLocation = locations[0]

       let center = CLLocationCoordinate2D(latitude: Userlocation.coordinate.latitude
           , longitude: Userlocation.coordinate.longitude)


        let annotation = MKPointAnnotation() 
        annotation.coordinate = center 
        annotation.title = "title" 
        Annotation.subtitle = “Subtitle”
        mapView.addAnnotation(annotation)
        let region = MKCoordinateRegion(center: center, span:  MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

      mapView.setRegion(region, animated: true)

   }
相关问题