9

How to add the location not just NSString but with latitude and longitude ,so it shows a map too in the Calendar?

<EKCalendarItem>  

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

@property(nonatomic, copy) NSString *location;

Code :

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

Example

Metalhead1247
  • 1,978
  • 1
  • 17
  • 28

3 Answers3

16

The property structuredLocation is available on iOS 9, though EKEvent documentation doesn't mention it (Update: it's finally here) , but structuredLocation does exist in EKEvent public header file, and you can check it inside Xcode. No need to use KVC to set it after iOS 9.

Swift version as follows:

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
denkeni
  • 969
  • 1
  • 10
  • 22
6

It's pretty bizarre that there is no documentation for this, but this is how you add a geoLocation to a calendar event.

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"];
Michael E
  • 656
  • 2
  • 7
  • 13
  • I don't doubt that this works. But the documentation mentions the use of EKStructuredLocation to be specifically for setting Geofencing locations. Any issues that might arise with this solution? – SpacyRicochet May 08 '15 at 14:44
  • 1
    @spacyRicochet - I haven't had any problems with this method. From what I've played around with, this is the only solution for adding a map to your calendar event. I really think the issue is that the documentation needs to be updated. Either they implemented this functionality and it was a mistake, or they just simply forgot to include this in their documentation. Regardless, it works. – Michael E May 08 '15 at 17:58
  • Doesn't seem to work properly for me, though that just might be Simulator not liking it. Will try it on device later. – SpacyRicochet May 09 '15 at 08:14
  • @SpacyRicochet it works on the iPhone 5. I haven't tested it on other devices. – Michael E May 10 '15 at 22:59
  • Swift, Xcode 13.4.1, setting this EKEvent this still returns an Unknown location when opening the EKEventEditViewController for valid CLLocationCoordinate2D and having plist entries for usage description. Using defaultCalendarForNewEvents. Anyone else experiencing this? – iOS Blacksmith Sep 16 '22 at 06:55
0

You might be able to use setValue:ForKey: on the EKEvent after creating a EKStructuredLocation, the key is 'structuredLocation'

Andrew Bennett
  • 910
  • 11
  • 11