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

前端 未结 8 1971
死守一世寂寞
死守一世寂寞 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 18:16

    It's easy to add a circle. Conform to MKMapViewDelegate. follow the bellow steps,,,

    Step 1 :

     CLLocationCoordinate2D center= {self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude};
    // Add an overlay
    MKCircle *circle= [MKCircle circleWithCenterCoordinate:center radius: 20000];//your distance like 20000(like meters)
    [myMapView addOverlay:circle];
    

    Step 2 :

     - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
     {
        MKCircleView *C_View = [[MKCircleView alloc] initWithOverlay:overlay];
        [C_View setFillColor:[UIColor lightGrayColor]];
        [C_View setStrokeColor:[UIColor blackColor]];
        [C_View setAlpha:0.5f];
    
        return C_View;
     }
    
    0 讨论(0)
  • 2020-11-29 18:19

    Swift 3/ Xcode 8 here:

    func addRadiusCircle(location: CLLocation){
        if let poll = self.selectedPoll {
            self.mapView.delegate = self
            let circle = MKCircle(center: location.coordinate, radius: 10)
            self.mapView.add(circle)
        }
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay is MKCircle {
            let circle = MKCircleRenderer(overlay: overlay)
            circle.strokeColor = UIColor.red
            circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
            circle.lineWidth = 1
            return circle
        } else {
            return MKPolylineRenderer()
        }
    }
    

    Then call like so:

    self.addRadiusCircle(location: CLLocation(latitude: YOUR_LAT_HERE, longitude: YOUR_LNG_HERE))
    
    0 讨论(0)
提交回复
热议问题