gpt4 book ai didi

ios - 使用 Swift 绘制折线

转载 作者:IT王子 更新时间:2023-10-29 05:41:24 25 4
gpt4 key购买 nike

我正在尝试了解如何使用 Swift 绘制多段线。我查看了文档,引用了一些教程,并查看了其他一些 SO 帖子,但我仍然无法在我的 map 上画一条线。这是我的代码。有人告诉我我在这里做错了什么吗?

import UIKit
import MapKit

class FirstViewController: UIViewController {

@IBOutlet weak var map: MKMapView!

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

let location = CLLocationCoordinate2D(
latitude: -73.761105,
longitude: 41.017791
)

let span = MKCoordinateSpanMake(0.07, 0.07)
let region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)

let annotation = MKPointAnnotation()
annotation.setCoordinate(location)
annotation.title = "White Plains"
annotation.subtitle = "Westchester County"
map.addAnnotation(annotation)

var locations = [CLLocation(latitude: -73.761105, longitude: 41.017791), CLLocation(latitude: -73.760701,longitude: 41.019348), CLLocation(latitude: -73.757201, longitude: 41.019267), CLLocation(latitude: -73.757482, longitude: 41.016375), CLLocation(latitude: -73.761105, longitude: 41.017791)]
var coordinates = locations.map({(location: CLLocation!) -> CLLocationCoordinate2D in return location.coordinate})
var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

self.map.addOverlay(polyline)

}

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}

return nil
}

}

谢谢!

最佳答案

此处 MKGeodesicPolyline 将解决您的问题。添加对象 MKGeodesicPolyline 而不是 MKPolyline

在您的代码中删除以下两行:

    let polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
map.add(polyline)

并添加这些行:

    let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: 2)
map.addOverlay(geodesic)

swift 5.0:

func createPolyline(mapView: MKMapView) {
let point1 = CLLocationCoordinate2DMake(-73.761105, 41.017791);
let point2 = CLLocationCoordinate2DMake(-73.760701, 41.019348);
let point3 = CLLocationCoordinate2DMake(-73.757201, 41.019267);
let point4 = CLLocationCoordinate2DMake(-73.757482, 41.016375);
let point5 = CLLocationCoordinate2DMake(-73.761105, 41.017791);

let points: [CLLocationCoordinate2D]
points = [point1, point2, point3, point4, point5]

let geodesic = MKGeodesicPolyline(coordinates: points, count: 5)
map.addOverlay(geodesic)

UIView.animate(withDuration: 1.5, animations: { () -> Void in
let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
let region1 = MKCoordinateRegion(center: point1, span: span)
self.map.setRegion(region1, animated: true)
})
}

objective-c 代码:

- (void) createGeoPolyline {

CLLocationCoordinate2D point1 = { -73.761105, 41.017791 };
CLLocationCoordinate2D point2 = { -73.760701, 41.019348 };
CLLocationCoordinate2D point3 = { -73.757201, 41.019267 };
CLLocationCoordinate2D point4 = { -73.757482, 41.016375 };
CLLocationCoordinate2D point5 = { -73.761105, 41.017791 };

CLLocationCoordinate2D points[] = {point1, point2, point3, point4, point5};

MKGeodesicPolyline *geodesic = [MKGeodesicPolyline polylineWithCoordinates:&points[0] count:5];
[self.mapView addOverlay:geodesic];

[UIView animateWithDuration:1.5 animations:^{
MKCoordinateRegion region;
region.center = point1;

MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
[self.mapView setRegion:region animated:YES];
}];
}

上面的 Objective C 代码运行完美,它会在下面显示叠加层:

enter image description here

但如果您尝试使用 Swift 代码,它不会。我尽我所能去解决它,但它不会改变。可能是 MapKit 框架的错误。

关于ios - 使用 Swift 绘制折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558912/

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