如何向日历条目添加位置?

时间:2018-02-03 12:42:44

标签: ios iphone swift calendar

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

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

如何在条目中添加(纬度/经度) - 坐标?使用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>"

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分: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