8

如何不仅添加 NSString 的位置,还添加纬度和经度,以便在日历中也显示地图?

<EKCalendarItem>  

https://developer.apple.com/LIBRARY/ios/documentation/EventKit/Reference/EKCalendarItemClassRef/index.html#//apple_ref/occ/instp/EKCalendarItem/location

@property(nonatomic, copy) NSString *location;

代码 :

 EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"Event Title";
    event.startDate = [NSDate date]; //today
    event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
    event.notes=@"Note";
    event.location=@"Eiffel Tower,Paris"; //how do i add Lat & long / CLLocation?
    event.URL=[NSURL  URLWithString:shareUrl];
    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
    NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
}];

例子

4

3 回答 3

15

该属性structuredLocation在 iOS 9 上可用,虽然 EKEvent 文档没有提到它(更新:终于在这里),但structuredLocation确实存在于 EKEvent 公共头文件中,您可以在 Xcode 中检查它。iOS 9 后无需使用 KVC 设置。

斯威夫特版本如下:

let location = CLLocation(latitude: 25.0340, longitude: 121.5645)
let structuredLocation = EKStructuredLocation(title: placeName)  // same title with ekEvent.location
structuredLocation.geoLocation = location
ekEvent.structuredLocation = structuredLocation
于 2016-05-25T08:03:26.677 回答
6

没有这方面的文档非常奇怪,但这就是您将地理位置添加到日历事件的方式。

EKStructuredLocation* structuredLocation = [EKStructuredLocation locationWithTitle:@"Location"]; // locationWithTitle has the same behavior as event.location
CLLocation* location = [[CLLocation alloc] initWithLatitude:0.0 longitude:0.0];
structuredLocation.geoLocation = location;

[event setValue:structuredLocation forKey:@"structuredLocation"];
于 2015-04-28T03:19:28.367 回答
0

在创建 EKStructuredLocation 后,您可能可以在 EKEvent 上使用 setValue:ForKey:,键是“structuredLocation”

于 2015-03-20T01:09:29.760 回答