d0ping / DBMapSelectorViewController

This component allows you to select circular map region from the MKMapView.
MIT License
323 stars 36 forks source link

DBMapSelectorViewController

Version License Platform Language

This component allows you to select circular map region from the MKMapView.

Screenshot of Example

Adding to your project

CocoaPods

To add DBMapSelectorViewController via CocoaPods into your project:

  1. Add a pod entry for DBMapSelectorViewController to your Podfile pod 'DBMapSelectorViewController', '~> 1.2.0'
  2. Install the pod by running pod install

Source Files

To add DBMapSelectorViewController manually into your project:

  1. Download the latest code, using git clone
  2. Open your project in Xcode, then drag and drop entire contents of the Source folder into your project (Make sure to select Copy items when asked if you extracted the code archive outside of your project)

Usage

To use DBMapSelectorViewController in your project you should perform the following steps:

  1. Import DBMapSelectorManager.h on your UIViewController subclass. Your class must include MKMapView instance and be his delegate.
  2. In your class implementation create instance of DBMapSelectorManager class. In Initialization method specify mapView instance. Assign your class as the delegate mapSelectorManager if needed.
  3. After initialization, set the initial map selector settings (center and radius) and apply settings.
  4. Forward following messages mapView delegate by the MapSelectorManager instance.
...
// (1)
#import "DBMapSelectorManager.h"

@interface ViewController () <DBMapSelectorManagerDelegate>
@property (nonatomic, strong) DBMapSelectorManager *mapSelectorManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // (2)
    self.mapSelectorManager = [[DBMapSelectorManager alloc] initWithMapView:self.mapView];
    self.mapSelectorManager.delegate = self;

    // (3)
    self.mapSelectorManager.circleCoordinate = CLLocationCoordinate2DMake(55.75399400, 37.62209300);
    self.mapSelectorManager.circleRadius = 3000;
    [self.mapSelectorManager applySelectorSettings];
}

...

// (4)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    return [self.mapSelectorManager mapView:mapView viewForAnnotation:annotation];
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
    [self.mapSelectorManager mapView:mapView annotationView:annotationView didChangeDragState:newState fromOldState:oldState];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay {
    return [self.mapSelectorManager mapView:mapView rendererForOverlay:overlay];
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [self.mapSelectorManager mapView:mapView regionDidChangeAnimated:animated];
}

@end

Property list selector

You can change additional MapSelector properties. Full properties list is shown below:

DBMapSelectorManagerDelegate

To be able to react when the main properties (coordinate and radius) of the selector will be changed you must become delegate DBMapSelectorManager. DBMapSelectorManagerDelegate protocol you can see here:

@protocol DBMapSelectorManagerDelegate <NSObject>

@optional
- (void)mapSelectorManager:(DBMapSelectorManager *)mapSelectorManager didChangeCoordinate:(CLLocationCoordinate2D)coordinate;
- (void)mapSelectorManager:(DBMapSelectorManager *)mapSelectorManager didChangeRadius:(CLLocationDistance)radius;
- (void)mapSelectorManagerWillBeginHandlingUserInteraction:(DBMapSelectorManager *)mapSelectorManager;
- (void)mapSelectorManagerDidHandleUserInteraction:(DBMapSelectorManager *)mapSelectorManager;

@end

You can implement these methods in your MyViewController class in order to respond to these changes. For example, how it can be implemented in your class:

- (void)mapSelectorManager:(DBMapSelectorManager *)mapSelectorManager didChangeCoordinate:(CLLocationCoordinate2D)coordinate {
    _coordinateLabel.text = [NSString stringWithFormat:@"Coordinate = {%.5f, %.5f}", coordinate.latitude, coordinate.longitude];
}

- (void)mapSelectorManager:(DBMapSelectorManager *)mapSelectorManager didChangeRadius:(CLLocationDistance)radius {
    NSString *radiusStr = (radius >= 1000) ? [NSString stringWithFormat:@"%.1f km", radius * .001f] : [NSString stringWithFormat:@"%.0f m", radius];
    _radiusLabel.text = [@"Radius = " stringByAppendingString:radiusStr];
}

Version history

1.2.2

1.2.1

1.2.0

1.1.0

Contact

Denis Bogatyrev (maintainer)

License

DBMapSelectorViewController - Copyright (c) 2015 Denis Bogatyrev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.