前面我介绍了如何调起第三方地图导航,那么我今天再介绍一下如何使用系统MapKit在地图上绘制路线,这个功能可能在地图上不经常使用,但是还是有必要和打家分享一下,希望可以帮助有需要的开发者!(swift)
看代码:
import UIKit
import MapKit
class MapVC: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
var geocoder : CLGeocoder = CLGeocoder()//编码器
var startPlacemark : CLPlacemark!//起始位置信息
var endPlacemark : CLPlacemark!//终点位置信息
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.mapView.delegate = self
self.mapView.zoomLevel = 15
self.setSpecialRegion(22.648838, longitude: 114.026178)
}
/** 显示特定区域 */
func setSpecialRegion(latitude:CLLocationDegrees, longitude:CLLocationDegrees) {
let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
let span = MKCoordinateSpanMake(0.01, 0.01)
let region = MKCoordinateRegionMake(coordinate, span)
self.mapView.setRegion(region, animated: true)
}
/** MKMapViewDelegate */
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let gradientColors = [UIColor.redColor()]
let polylineRenderer = JLTGradientPathRenderer(polyline: overlay as! MKPolyline, colors: gradientColors)
polylineRenderer.lineWidth = 4
return polylineRenderer
}
/** 根据位置名称编码获取位置信息 */
func getStartPlacemark(latitude:CLLocationDegrees, longtitude:CLLocationDegrees) {
let location = CLLocation.init(latitude: latitude, longitude: longtitude)
CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
if (error != nil || placemarks!.count == 0) {
print("对不起,位置信息错误")
}else{
self.startPlacemark = placemarks?.first
self.mapView.addAnnotation(self.setAnnotationView((self.startPlacemark.location?.coordinate.latitude)!, longtitude: (self.startPlacemark.location?.coordinate.longitude)!, text: self.startPlacemark.name!))
}
}
}
func getEndPlacemark(latitude:CLLocationDegrees, longtitude:CLLocationDegrees) {
let location = CLLocation.init(latitude: latitude, longitude: longtitude)
CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
if (error != nil || placemarks!.count == 0) {
print("对不起,位置信息错误")
}else{
self.endPlacemark = placemarks?.first
self.mapView.addAnnotation(self.setAnnotationView((self.endPlacemark.location?.coordinate.latitude)!, longtitude: (self.endPlacemark.location?.coordinate.longitude)!, text: self.endPlacemark.name!))
}
}
}
///设置大头针
func setAnnotationView(latitude:CLLocationDegrees,longtitude:CLLocationDegrees,text:String) -> MKPointAnnotation{
let coordinate = CLLocationCoordinate2DMake(latitude, longtitude)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = text
return annotation
}
@IBAction func action(sender: UIButton) {
getStartPlacemark(22.648838, longtitude: 114.026178)
getEndPlacemark(22.658838, longtitude: 114.036178)
}
/** 画线方法 */
func drawLine() {
let request = MKDirectionsRequest()
let startMKP = MKPlacemark.init(placemark: startPlacemark)
request.source = MKMapItem.init(placemark: startMKP)
let endMKP = MKPlacemark.init(placemark: endPlacemark)
request.destination = MKMapItem.init(placemark: endMKP)
let directions = MKDirections.init(request: request)
directions.calculateDirectionsWithCompletionHandler { (response, error) in
if error == nil {
//计算正确
for route:MKRoute in (response?.routes)! {
self.mapView.addOverlay(route.polyline)
}
}else{
print("计算路线错误")
}
}
}
@IBAction func drawLineAction(sender: UIButton) {
if self.startPlacemark != nil && self.endPlacemark != nil {
drawLine()
}
}
效果图: