---
agentTools:
  projectIndex: https://operations.readme.io/llms.txt
---

# Location Operation(s)

Location Services wrapped up in Operations

Operations has support for location services on iOS.

[block:api-header]
{
  "type": "basic",
  "title": "User Location"
}
[/block]

`UserLocationOperation` can retrieve the location of the user's device. Internally it adds an `AuthorizedFor` condition using `Capability.Location`. See the section on [Capabilities](https://operations.readme.io/docs/capabilities) for more detailed information, but for now, it's enough to know that the operation will handle authorization for location services automatically.

The class can be used with a completion block as in the example below

[block:code]
{
  "codes": [
    {
      "code": "let operation = UserLocationOperation { location in\n    print(\"Location: \\(location)\")\n}\nqueue.addOperation(operation)",
      "language": "swift",
      "name": "user location with completion block"
    }
  ]
}
[/block]

Alternatively, `UserLocationOperation` sets the resultant `CLLocation` as a property: `location`.

[block:code]
{
  "codes": [
    {
      "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)",
      "language": "swift",
      "name": "accessing property of user location"
    }
  ]
}
[/block]

The 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](https://operations.readme.io/docs/groups) for more guidance on creating groups.

[block:api-header]
{
  "type": "basic",
  "title": "Reverse Geocode"
}
[/block]

Core 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`.

In Operations, this can be used with a `ReverseGeocodeOperation`:

[block:code]
{
  "codes": [
    {
      "code": "let operation = ReverseGeocodeOperation(location: aLocation) { placemark in\n    print(\"Placemark: \\(placemark)\")\n}\nqueue.addOperation(operation)",
      "language": "swift",
      "name": "reverse geocode"
    }
  ]
}
[/block]

It also stores the received placemark in a property similar to `UserLocationOperation`.

[block:api-header]
{
  "type": "basic",
  "title": "Reverse Geocode User Location"
}
[/block]

Putting 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.

[block:code]
{
  "codes": [
    {
      "code": "let operation = ReverseGeocodeUserLocation { location, placemark in\n    print(\"Placemark: \\(placemark)\")\n}\nqueue.addOperation(operation)",
      "language": "swift",
      "name": "reverse geocode user location"
    }
  ]
}
[/block]

[block:callout]
{
  "type": "info",
  "title": "Accuracy",
  "body": "Both `UserLocationOperation` and `ReverseGeocodeUserLocation` accept a `CLLocationAccuracy` argument which has a sensible default value of three kilometers."
}
[/block]

[block:callout]
{
  "type": "success",
  "title": "Chain operations together with Result Injection",
  "body": "All location based operations conform to `ResultOperationType` which means their results can be injected as the requirement of other operations. See [Injecting  Results](https://operations.readme.io/docs/injecting-results)."
}
[/block]