{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Location Operation(s)","type":"basic","slug":"location-operation","excerpt":"Location Services wrapped up in Operations","body":"Operations has support for location services on iOS.\n[block:api-header]\n{\n \"type\": \"basic\",\n \"title\": \"User Location\"\n}\n[/block]\n`UserLocationOperation` can retrieve the location of the user's device. Internally it adds an `AuthorizedFor` condition using `Capability.Location`. See the section on [Capabilities](doc:capabilities) for more detailed information, but for now, it's enough to know that the operation will handle authorization for location services automatically.\n\nThe class can be used with a completion block as in the example below\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"let operation = UserLocationOperation { location in\\n print(\\\"Location: \\\\(location)\\\")\\n}\\nqueue.addOperation(operation)\",\n \"language\": \"swift\",\n \"name\": \"user location with completion block\"\n }\n ]\n}\n[/block]\nAlternatively, `UserLocationOperation` sets the resultant `CLLocation` as a property: `location`.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"let operation = UserLocationOperation()\\noperation.addObserver(FinishedObserver { op in\\n if let op = op as? UserLocationOperation {\\n print(\\\"Location: \\\\(op.location)\\\") \\n }\\n})\\nqueue.addOperation(operation)\",\n \"language\": \"swift\",\n \"name\": \"accessing property of user location\"\n }\n ]\n}\n[/block]\nThe above example is little redundant. A more likely usage of the `location` property would be when the operation is used inside of a `GroupOperation` subclass. See [Groups](doc:groups) for more guidance on creating groups.\n[block:api-header]\n{\n \"type\": \"basic\",\n \"title\": \"Reverse Geocode\"\n}\n[/block]\nCore Location supports [geocoding](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/index.html#//apple_ref/occ/cl/CLGeocoder) functionality. Reverse geocoding is where a known location is looked up to retrieve human readable location information such as an address. The actual result type for this information is a `CLPlacemark`.\n\nIn Operations, this can be used with a `ReverseGeocodeOperation`:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"let operation = ReverseGeocodeOperation(location: aLocation) { placemark in\\n print(\\\"Placemark: \\\\(placemark)\\\")\\n}\\nqueue.addOperation(operation)\",\n \"language\": \"swift\",\n \"name\": \"reverse geocode\"\n }\n ]\n}\n[/block]\nIt also stores the received placemark in a property similar to `UserLocationOperation`.\n[block:api-header]\n{\n \"type\": \"basic\",\n \"title\": \"Reverse Geocode User Location\"\n}\n[/block]\nPutting this all together, we have `ReverseGeocodeUserLocation` which first get the device's current location and then use that for a reverse geocode lookup. Therefore, if you functionality in your app where the user's can enter their current address, it might make sense to use this operation. The returned `CLPlacemark` can return a localized address information.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"let operation = ReverseGeocodeUserLocation { location, placemark in\\n print(\\\"Placemark: \\\\(placemark)\\\")\\n}\\nqueue.addOperation(operation)\",\n \"language\": \"swift\",\n \"name\": \"reverse geocode user location\"\n }\n ]\n}\n[/block]\n\n[block:callout]\n{\n \"type\": \"info\",\n \"title\": \"Accuracy\",\n \"body\": \"Both `UserLocationOperation` and `ReverseGeocodeUserLocation` accept a `CLLocationAccuracy` argument which has a sensible default value of three kilometers.\"\n}\n[/block]\n\n[block:callout]\n{\n \"type\": \"success\",\n \"title\": \"Chain operations together with Result Injection\",\n \"body\": \"All location based operations conform to `ResultOperationType` which means their results can be injected as the requirement of other operations. See [Injecting Results](doc:injecting-results).\"\n}\n[/block]","updates":[],"order":2,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"567401b522cd7e0d00fada86","category":{"sync":{"isSync":false,"url":""},"pages":["56740186f79ca90d00ad2f68","5674019122cd7e0d00fada83","567401b522cd7e0d00fada86"],"title":"iOS only Operations","slug":"ios-only-operations","order":3,"from_sync":false,"reference":false,"_id":"5674017bf79ca90d00ad2f67","project":"5672fc989996590d00c22c65","version":"5672fc989996590d00c22c68","createdAt":"2015-12-18T12:52:11.346Z","__v":3},"createdAt":"2015-12-18T12:53:09.836Z","version":{"version":"2.5.0","version_clean":"2.5.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5672fc999996590d00c22c69","567301169d4c060d009da8b3","56730183547bee0d00997d1a","5673018a06b19d0d0010691b","567301b53054630d00fe9288","567400638565060d009a86fb","5674017bf79ca90d00ad2f67"],"_id":"5672fc989996590d00c22c68","__v":7,"createdAt":"2015-12-17T18:19:04.699Z","project":"5672fc989996590d00c22c65","releaseDate":"2015-12-17T18:19:04.699Z"},"__v":17,"githubsync":"","project":"5672fc989996590d00c22c65","user":"5654ea8be0d82b0d00ab5747"}
Location Operation(s)
Location Services wrapped up in Operations