Once you have these, specifying a region to show is relatively easy. Let's say I want to show approximately a half mile around a given location in my Core Location delegate method (I know MapView will track location for you, this is just a convenient way to show how to use the macros):
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
These aren't exact, but they should be close enough. Although the distance represented by one degree latitude isn't a constant, it varies by a small enough amount as to be insignificant for this calculation. For longitude, a degree at the equator is substantially larger than one degree in the arctic circle, so we have to use a little math to get the approximate number.
That's it. With this code, it will zoom down to the current location and show roughly a quarter mile on each side of the current location. The call to adjust region will change that a little to match the view's aspect ratio. Want to zoom out? Change 0.5 to a larger value. If you use 10, it will show about five miles to each side of the current location.
Whoops! Turns out there's already a built-in function that works similarly to these macros: MKCoordinateRegionMakeWithDistance()
No comments:
Post a Comment