gpt4 book ai didi

ios - 用户位置的简单 MKCircle 实现

转载 作者:行者123 更新时间:2023-11-28 07:34:33 26 4
gpt4 key购买 nike

IOS 和 SWIFT 的真正新手。试图在当前位置上覆盖一个圆圈。以下代码生成 map View ,在用户位置放置一个蓝点但不绘制叠加圆圈。请帮忙。

//
// ViewController.swift
// userLocation
//

//

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

//Map outlet
@IBOutlet weak var map: MKMapView!
@IBOutlet weak var zoomFactorStepper: UIStepper!

let manager = CLLocationManager()
var zoomFactor = 0.01

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: zoomFactor, longitudeDelta: zoomFactor)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegion(center: myLocation, span: span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
map.removeOverlays(map.overlays)
let circle = MKCircle(center: location.coordinate, radius: 100)
map.addOverlay(circle)

}

这个函数完成主要工作。

    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()

}

@IBAction func zoomFactorStepperAction(_ sender: Any) {
zoomFactor = zoomFactorStepper.value * 0.01
//map.setNeedsDisplay()
}

func map(_ map: 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
}

}

最佳答案

这是缺失的,一旦我添加了它,圆圈就会按预期下降。

map.delegate = self

关于ios - 用户位置的简单 MKCircle 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53573944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com