使用Swift绘制折线 [英] Draw polyline using Swift
问题描述
我试图了解如何使用Swift绘制折线。我查看了文档,引用了一些教程,并查看了其他一些SO帖子,但我仍然无法在地图上画一条线。这是我的代码。有谁告诉我这里我做错了什么?
I'm trying to get an understanding of how to draw polylines using Swift. I've looked at the documentation, referenced some tutorials, and checked out some other SO posts, but I still can't get the thing to draw a line on my map. Here's my code. Anyone tell me what I'm doing wrong here?
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
。
Here MKGeodesicPolyline
will solve your problem. Add object of MKGeodesicPolyline
instead of MKPolyline
.
In你的代码删除两行以下:
In your code remove below two lines:
let polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
map.add(polyline)
并添加以下行:
let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: 2)
map.add(geodesic)
Swift代码:
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.add(geodesic)
UIView.animate(withDuration: 1.5, animations: { () -> Void in
let span = MKCoordinateSpanMake(0.01, 0.01)
let region1 = MKCoordinateRegion(center: point1, span: span)
self.map.setRegion(region1, animated: true)
})
}
目标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];
}];
}
以上目标C代码完美无缺,它将在下面显示叠加:
Above Objective C code works perfect and it will show overlay below:
但是如果你尝试使用Swift代码,它就不会。我尽可能多地尝试解决它,但它不会改变。可能是MapKit框架的bug。
But if you try Swift code it will not. I tried as much as I can to solve it out but It won't change. May be it is bug from MapKit framework.
这篇关于使用Swift绘制折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!