我有两组坐标并试图在地图视图上绘制折线。我可以用一组坐标绘制一条折线,但我无法绘制两条折线。
下面是代码...
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake(icRouteLat[0], icRouteLong[0])
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
routePointer = "one";
// first route line
//----------able to draw polyline with this set of coordinates
for i in 0..<routeLat.count-1
{
var fromCoordinate :CLLocation = CLLocation(latitude: routeLat[i], longitude: routeLong[i])
var toCoordinate :CLLocation = CLLocation(latitude: routeLat[i+1], longitude: routeLong[i+1])
var locations = [fromCoordinate, toCoordinate];
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
var polyLine = MKPolyline(coordinates: &coordinates, count: locations.count);
mapView.addOverlay(polyLine);
}
routePointer = "second";
// IC route line
//------Polyline for this set of coordinates doesn't appear on map view
for j in 0..<icRouteLat.count-1
{
var fromCoordinateIC :CLLocation = CLLocation(latitude: icRouteLat[j], longitude: icRouteLat[j])
var toCoordinateIC :CLLocation = CLLocation(latitude: icRouteLong[j+1], longitude: icRouteLong[j+1])
var locationsIC = [fromCoordinateIC, toCoordinateIC];
var coordinatesIC = locationsIC.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
var polyLineIC = MKPolyline(coordinates: &coordinatesIC, count: locationsIC.count);
mapView.addOverlay(polyLineIC);
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
let route: MKPolyline = overlay as MKPolyline
let routeRenderer = MKPolylineRenderer(polyline:route)
routeRenderer.lineWidth = 3.0
if routePointer == "one"
{
routeRenderer.strokeColor = UIColor(red: 240.0/255.0, green: 68.0/255.0, blue: 0.0/255.0, alpha: 1);
}
else if routePointer == "second"
{
routeRenderer.strokeColor = UIColor(red: 45.0/255.0, green: 200.0/255.0, blue: 0.0/255.0, alpha: 1);
}
return routeRenderer
}
数组routeLat[], routeLong[]
和icRouteLat[] and icRouteLong[]
是两组坐标。
我在这里有什么遗漏吗?或者这是正确的实施方式?
编辑
根据建议更新了代码...
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake(icRouteLat[0], icRouteLong[0])
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
var polyLine: MKPolyline!
var polyLineIC: MKPolyline!
// first route line
for i in 0..<routeLat.count-1
{
var fromCoordinate :CLLocation = CLLocation(latitude: routeLat[i], longitude: routeLong[i])
var toCoordinate :CLLocation = CLLocation(latitude: routeLat[i+1], longitude: routeLong[i+1])
var locations = [fromCoordinate, toCoordinate];
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
polyLine = MKPolyline(coordinates: &coordinates, count: locations.count);
polyLine.title = "one";
mapView.addOverlay(polyLine);
}
// IC route line
for j in 0..<icRouteLat.count-1
{
var fromCoordinateIC :CLLocation = CLLocation(latitude: icRouteLat[j], longitude: icRouteLong[j])
var toCoordinateIC :CLLocation = CLLocation(latitude: icRouteLat[j+1], longitude: icRouteLong[j+1])
var locationsIC = [fromCoordinateIC, toCoordinateIC];
var coordinatesIC = locationsIC.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
polyLineIC = MKPolyline(coordinates: &coordinatesIC, count: locationsIC.count);
polyLineIC.title = "ic";
mapView.addOverlay(polyLineIC);
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline
{
let route: MKPolyline = overlay as MKPolyline
let routeRenderer = MKPolylineRenderer(polyline:route)
routeRenderer.lineWidth = 3.0
if overlay.title == "one"
{
routeRenderer.strokeColor = UIColor(red: 240.0/255.0, green: 68.0/255.0, blue: 0.0/255.0, alpha: 1);
}
else if overlay.title == "ic"
{
routeRenderer.strokeColor = UIColor(red: 45.0/255.0, green: 200.0/255.0, blue: 0.0/255.0, alpha: 1);
}
return routeRenderer
}
return nil
}
最终编辑
更新代码:
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = icRoute[0];
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
// first route line
polyLine = MKPolyline(coordinates: &firstRoute, count: firstRoute.count);
polyLine.title = "one";
mapView.addOverlay(polyLine);
// IC route line
polyLine = MKPolyline(coordinates: &icRoute, count: icRoute.count);
polyLine.title = "ic";
mapView.addOverlay(polyLine);
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline
{
let route: MKPolyline = overlay as MKPolyline
let routeRenderer = MKPolylineRenderer(polyline:route)
routeRenderer.lineWidth = 3.0
if overlay.title == "one"
{
routeRenderer.strokeColor = UIColor(red: 240.0/255.0, green: 68.0/255.0, blue: 0.0/255.0, alpha: 1);
}
else if overlay.title == "ic"
{
routeRenderer.strokeColor = UIColor(red: 45.0/255.0, green: 200.0/255.0, blue: 0.0/255.0, alpha: 1);
}
return routeRenderer
}
return nil
}