【问题标题】:How to add location to calendar-entry?如何将位置添加到日历条目?
【发布时间】:2018-02-03 12:42:44
【问题描述】:

您可以在下面找到我用来向用户日历添加条目的代码。

我想知道如何在条目中添加位置标记 - 我找到了event.structuredLocation 属性。

如何在条目中添加 (lat/lon) - 坐标?使用 CLLocationCoordinate2D(lat:, lon:) 不起作用。

func addReminder() {
    let eventStore : EKEventStore = EKEventStore()
    let event:EKEvent = EKEvent(eventStore: eventStore)
    event.title = "myevent"
    event.startDate = Date()
    event.endDate = Date()
    event.calendar = eventStore.defaultCalendarForNewEvents
    event.structuredLocation = CLLocationCoordinate2D(latitude: 1.0, longitude: 2.0) // not working
    do {
        try eventStore.save(event, span: .thisEvent)
    } catch let error as NSError {
        print("error : \(error)")
    }
}


编辑 (1) :我确定我必须使用 EKStructuredLocation。但我也不知道如何实现这一点? (EKStructuredLocation(mapItem: MKMapItem))

编辑(2)

let map = MKMapItem()
map.name = "myplace"
map.placemark.coordinate.latitude = 10.0 // error
map.placemark.coordinate.longitude = 10.0 // error

编辑(3)

是否可以在 event.notes 属性中添加粗体

event.notes = "default text and bold text" // like html 
//"default text and <b>bold text</b>"

任何帮助将不胜感激:)

【问题讨论】:

  • 我为您提供了编辑 2 的替代方法,您可以使用 CLLocation 而不是 MKMapItem 创建结构化位置。至于您关于包含粗体文本的注释的问题,答案似乎是否定的。 notes 属性是String,而不是AttributedString,我根本看不到任何属性字符串属性。此外,日历应用似乎不支持备注字段中的样式文本。

标签: ios iphone swift calendar


【解决方案1】:

我以前没有使用过它,但是如果您查看 EKStructuredLocation 的文档,它有一个带有标题的初始化程序,并且它有 3 个属性:

var title: String? ///The title of the location.
var geoLocation: CLLocation? //The core location.
var radius: Double ///A minimum distance from the core location that would trigger the alarm or reminder.

所以你会使用这样的代码:

var structuredLocation = EKStructuredLocation(title:"title") //Put your title here

let location = CLLocation(latitude: lat, longitude: long) //use your lat/long vals
structuredLocation.geoLocation = location

structuredLocation.radius = 1000 //This would be a 1 KM distance. Modify as desired

event.structuredLocation = structuredLocation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多