我创建了一个应用程序,其中使用(MapKit)绘制了地图,当我按“开始”时,它将自动缩放到我的位置并放置注释。
但是从这里开始,我很困惑。我正在尝试从起始位置到当前位置绘制一条折线。 (并更新我做的每一个动作)。举个例子,如果我向北走300米,我应该可以在手机上查看地图,然后看到折线在跟着我。
因此,从注释----->(折线)开始,直到用户。并始终保持更新(这样您就可以看到行在移动
我该怎么做?如果您知道,请在评论中让我知道。我会非常感激! :)
用于在正确位置添加注释的代码:
@IBAction func StartWalk(_ sender: Any)
{
if play == true
{
play = false
//Set resetbutton disabled.
ResetButton.isHidden = true
//Set new image when play is true
PlayStop.setImage(UIImage(named: "Stop"), for: .normal)
//Bool to check if button is stopped (op)
isStopped = false
//Checking userpermission to allow map and current location
if (CLLocationManager.locationServicesEnabled())
{
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
//Retrieve current position
if let userLocation = locationManager.location?.coordinate
{
//Zooming in to current position
let viewRegion = MKCoordinateRegion(center: userLocation, latitudinalMeters: 200, longitudinalMeters: 200)
mapView.setRegion(viewRegion, animated: false)
//Creating a start annotation
let annotation = MKPointAnnotation()
annotation.title = "Start"
annotation.coordinate = userLocation
mapView.addAnnotation(annotation)
}
}
}
}
以下是折线的想法:
//Create polyline
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
{
if(overlay is MKPolyline)
{
let polyLineRender = MKPolylineRenderer(overlay: overlay)
polyLineRender.strokeColor = UIColor.blue.withAlphaComponent(1)
polyLineRender.lineWidth = 3
return polyLineRender
}
return MKPolylineRenderer()
}
//Updating location + polylines
@objc func update()
{
//Startposition
let startLat = locationManager.location?.coordinate.latitude
let startlong = locationManager.location?.coordinate.longitude
let startResult = CLLocation(latitude: startLat!, longitude: startlong!)
//This should be the current user location.
let stopLat = locationManager.location?.coordinate.latitude
let stopLong = locationManager.location?.coordinate.longitude
let stopResult = CLLocation(latitude: stopLat!, longitude: stopLong!)
let locations =
[
CLLocationCoordinate2D(latitude: startLat!, longitude: startlong!),
CLLocationCoordinate2D(latitude: stopLat!, longitude: stopLong!)
]
//Draw polyline on the map
let aPolyLine = MKPolyline(coordinates: locations, count: locations.count)
//Adding polyline to mapview
mapView.addOverlay(aPolyLine)
}
简而言之:
我希望折线从起始位置开始,然后跟随用户所走的位置,直到按下停止按钮为止。就像一直追逐蓝点。你知道吗?请打我
答案 0 :(得分:1)
您是否在ViewController类上实现MKMapViewDelegate方法?
如果是,则可以从MapView访问委托,您必须在viewDidLoad func或任何其他您希望带有坐标的以下代码中添加:
mapView.delegate = self
// Connect all the mappoints using Poly line.
var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
for annotation in annotations {
points.append(annotation.coordinate)
}
var polyline = MKPolyline(coordinates: &points, count: points.count)
mapView.addOverlay(polyline)
您可以按照以下非常好的本教程:http://rshankar.com/how-to-add-mapview-annotation-and-draw-polyline-in-swift/
答案 1 :(得分:0)
设置为CLLocationManagerDelegate,CoreLocation将在位置可用时为您提供位置。例如:
import CoreLocation
class MyViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setupCoreLocation()
}
func setupCoreLocation() {
let locationManager = CLLocationManager()
locationManager.delegate = self
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .authorizedWhenInUse:
// NOTE: I normally only set up "WhenInUse" because "Always"
// can quickly drain the battery on a device.
locationManager.startUpdatingLocation()
default:
break
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Create and add your MKPolyline here based on locations
// passed (the last element of the array is the most recent
// position).
}
}