Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 地图注释的自定义标题_Ios_Mapkit - Fatal编程技术网

Ios 地图注释的自定义标题

Ios 地图注释的自定义标题,ios,mapkit,Ios,Mapkit,在“我的地图”应用程序中,用户可以在搜索栏中输入地址,并用红色pin显示位置。在我的代码中,是否有可能允许用户在将pin放置在地图上之前输入自定义标题和副标题?标题和副标题应显示在标注气泡中。这是怎么做到的?现在,我所有的pin标题都是“我的位置” 另外,如何将我的所有PIN保存到NSUserDefault,以便在应用程序重新启动时显示它们?这是可行的,还是只保存一个pin码 NSUserDefaults *defaults = [NSUserDefaults standardUserDefau

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

另外,如何将我的所有PIN保存到NSUserDefault,以便在应用程序重新启动时显示它们?这是可行的,还是只保存一个pin码

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];

我将感谢任何帮助!谢谢

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

NSString *title = [self.titleField stringValue];
[annotation setTitle:@"title"];
NSString *subtitle = [self.subtitleField stringValue];
[annotation setSubtitle:@"title"];
但是,您可能没有显示三个文本字段的屏幕空间。什么是使用默认名称创建pin,然后允许用户打开详图索引编号,或者自动打开它并显示一个按钮来编辑它,然后在顶部显示一个模式对话框来编辑标题和副标题


如果您在
allPins\u latitude
键下保存了每个
coordinate.latitude
,则每个坐标都将覆盖上一个坐标,并且只保存最后一个坐标。如果您的PIN都存储为数组,请存储该数组。

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

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

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

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

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


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

@peter我认为下面的代码将对您有所帮助 //斯威夫特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)

   }

谢谢这很有帮助。不过我有一个问题。。。我只是开始学习如何使用地图工具包。我的详图索引视图已经有一个按钮,但如何使用此按钮显示模式对话框?我如何将PIN存储为数组?顺便说一句,我单击了您提供的链接。你的应用看起来不错。这两个问题都应该是Stackoverflow上的一个新条目,它可以帮助具有相同问题的其他人找到解决方案,但你要查找的调用是
mapView:annotationView:calloutAccessoryControlTapped:
当点击按钮时,它将转到该功能,你可以做任何你想做的事情,包括显示模态对话框。数组的东西很简单。添加新pin后,将其添加到阵列中。试一试,遇到问题时再问一个新问题。谢谢,我非常喜欢你的UIAlertView解决方案。我会努力做到这一点。至于MKAnnotation对象,我不知道如何做到这一点。你愿意分享一些示例代码吗,或者告诉我在哪里可以找到教程?我将不胜感激!!
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)

   }