Iv一直在四处寻找一个很好的解释,说明如何使用SWIFT2.0为MKCircle编写MapView注释,但我似乎找不到足够的解释。有人可以发布一些示例代码来展示如何创建MKCircle注释吗?这是我用来绘制地图和获取坐标的代码。
let address = self.location
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
self.locationCoordinates = coordinates
let span = MKCoordinateSpanMake(0.005, 0.005)
let region = MKCoordinateRegion(center: self.locationCoordinates, span: span)
self.CIMap.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = self.locationCoordinates
self.CIMap.addAnnotation(annotation)
self.CIMap.layer.cornerRadius = 10.0
self.CIMap.addOverlay(MKCircle(centerCoordinate: self.locationCoordinates, radius: 1000))
}
})发布于 2017-07-13 10:38:03
将展示如何使用xcode 8.3.3在地图视图上创建循环覆盖的步骤。
在您的主情节提要文件中,将mapView视图拖到情节提要的场景(视图)上,并为同样的场景创建出口,这里我创建了mapView。此外,无论何时在地图上按长键,您都希望动态地创建覆盖,因此将long press手势识别器从对象库拖到mapView上,然后为相同的操作方法创建操作方法,这里我为相同的对象创建了addRegion()。
不过,在地图上呈现圆圈之前,您不会在地图上实际看到圆圈。为此,您需要实现implement委托的委托。
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {}您应该在委托方法中调用locationManager.stopUpdatingLocation(),这样电池就不会耗尽。
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}
let circleRenderer = MKCircleRenderer(circle: circelOverLay)
circleRenderer.strokeColor = .blue
circleRenderer.fillColor = .blue
circleRenderer.alpha = 0.2
return circleRenderer
}
}这里我们在屏幕上画一个实际的圆圈。
最后的代码应该如下所示。
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
// MARK: Long Press Gesture Recognizer Action Method
@IBAction func addRegion(_ sender: Any) {
print("addregion pressed")
guard let longPress = sender as? UILongPressGestureRecognizer else {return}
let touchLocation = longPress.location(in: mapView)
let coordinates = mapView.convert(touchLocation, toCoordinateFrom: mapView)
let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")
mapView.removeOverlays(mapView.overlays)
locationManager.startMonitoring(for: region)
let circle = MKCircle(center: coordinates, radius: region.radius)
mapView.add(circle)
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
mapView.showsUserLocation = true
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}
let circleRenderer = MKCircleRenderer(circle: circelOverLay)
circleRenderer.strokeColor = .blue
circleRenderer.fillColor = .blue
circleRenderer.alpha = 0.2
return circleRenderer
}
}发布于 2015-10-23 00:57:06
首先,您需要将MKMapViewDelegate添加到类防御中。
mapView.delegate = self 在viewDidLoad中将映射委托设置为self。
设置注释
mapView.addOverlay(MKCircle(centerCoordinate: CLLocationCoordinate2D, radius: CLLocationDistance))现在应该在mapView委托中调用rendererForOverlay,并在那里绘制它。
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if let overlay = overlay as? MKCircle {
let circleRenderer = MKCircleRenderer(circle: overlay)
circleRenderer.fillColor = UIColor.blueColor()
return circleRenderer
}
}此外,您还需要导入MapKit以使其全部编译
发布于 2015-10-23 00:48:06
重叠仅仅是一组数字。在地图视图中可见的东西是一个覆盖渲染器。您必须实现mapView:rendererForOverlay:来提供覆盖渲染器;否则,您将什么也看不到。
https://stackoverflow.com/questions/33293075
复制相似问题