Draw a circle of 1000m radius around users location in MKMapView

前端 未结 8 1970
死守一世寂寞
死守一世寂寞 2020-11-29 17:18

(Using iOS 5 and Xcode 4.2)

I have an MKMapView and want to draw a circle of 1000m radius around the user location.

On the surface it would seem tha

相关标签:
8条回答
  • 2020-11-29 17:59

    Try to use the code from Apple Breadcrumb example

    0 讨论(0)
  • 2020-11-29 18:01

    Try a custom overlay. Add this in viewDidLoad:

    MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
    [map addOverlay:circle];
    

    userLocation can be obtained by storing the MKUserLocationAnnotation as a property. Then, to actually draw the circle, put this in the map view's delegate:

    - (MKOverlayRenderer *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
    {
        MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
        circleView.strokeColor = [UIColor redColor];
        circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
        return circleView;
    }
    
    0 讨论(0)
  • 2020-11-29 18:07

    An updated version for iOS 8.0 using Swift.

    import Foundation
    import MapKit
    
    class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
        var locationManager: CLLocationManager = CLLocationManager()
    
        @IBOutlet var mapView: MKMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // We use a predefined location
            var location = CLLocation(latitude: 46.7667 as CLLocationDegrees, longitude: 23.58 as CLLocationDegrees)
    
            addRadiusCircle(location)
        }
    
        func addRadiusCircle(location: CLLocation){
            self.mapView.delegate = self
            var circle = MKCircle(centerCoordinate: location.coordinate, radius: 10000 as CLLocationDistance)
            self.mapView.addOverlay(circle)
        }
    
        func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
            if overlay is MKCircle {
                var circle = MKCircleRenderer(overlay: overlay)
                circle.strokeColor = UIColor.redColor()
                circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
                circle.lineWidth = 1
                return circle
            } else {
                return nil
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:08

    I didn't understand benwad answer. So here is clearer answer:

    It's pretty easy to add a circle. Conform to MKMapViewDelegate

    @interface MyViewController : UIViewController <MKMapViewDelegate>
    @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    @end
    

    In viewDidLoad, Create a circle annotation and add it to the map:

    CLLocationCoordinate2D center = {39.0, -74.00};
    
    // Add an overlay
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:center radius:150000];
    [self.mapView addOverlay:circle];
    

    Then implement mapView:viewForOverlay: to return the view.

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
    {
        MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
        [circleView setFillColor:[UIColor redColor]];
        [circleView setStrokeColor:[UIColor blackColor]];
        [circleView setAlpha:0.5f];
        return circleView;
    }
    

    But if you want the circle to always be the same size, no matter the zoom level, you'll have to do something different. Like you say, in regionDidChange:animated:, get the latitudeDelta, then create a new circle (with a radius that fits into the width), remove the old one and add the new one.

    Note from me: don't forget to connect mapview with your view controller delegate. Otherwise viewForOverlay won't be called.

    0 讨论(0)
  • 2020-11-29 18:11

    All I did is, After displaying the location on the map kit called the below function in the end.

    @IBOutlet weak var mapView: GMSMapView!
    
    var cirlce: GMSCircle!
    
    override func viewDidLoad() {
    
        super.viewDidLoad()
        mapView.delegate = self
        circleview(redius: 5000) 
    
      }
    
    //used this func to draw the circle
    
     func circleview(redius:Double) {
    
        let  circleCenter = CLLocationCoordinate2D(latitude: 13.3450223, longitude: 74.7512519)
    
        cirlce = GMSCircle(position: circleCenter, radius: redius)
        cirlce.fillColor = UIColor(red: 230.0/255.0, green: 230.0/255.0, blue: 250.0/255.0, alpha:1.0)
        cirlce.strokeColor = .blue
        cirlce.strokeWidth = 2
        cirlce.map = mapView
      }
    
    0 讨论(0)
  • 2020-11-29 18:12
    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
    

    it is deprecated since iOS 4.0

    0 讨论(0)
提交回复
热议问题