我先廢話一下下重抖,一代學(xué)渣君浸船。最近要期末啦。木有時(shí)間學(xué)習(xí)ios啦鹤竭。
import UIKit
import MapKit
class ViewController: UIViewController ,MKMapViewDelegate {
var startField:UITextField!//出發(fā)地
var deretField:UITextField!//目的地
var nav:UIButton!//導(dǎo)航開(kāi)啟
var _mapView:MKMapView!
var _startLocation:CLLocation!//出發(fā)點(diǎn)位置信息
var _dereLocation:CLLocation!//目的地位置信息
var ge:CLGeocoder!//反編碼管理類
var NavPath:MKPolyline! //導(dǎo)航線路
var startMark:CLPlacemark! //出發(fā)點(diǎn)的相關(guān)信息
override func viewDidLoad() {
super.viewDidLoad()
self .loadMapView()
self.initSubViews()
// Do any additional setup after loading the view, typically from a nib.
}
func loadMapView()
{
_mapView = MKMapView(frame: self.view.bounds)
_mapView.mapType = MKMapType.Standard
_mapView.zoomEnabled = true
_mapView.scrollEnabled = true
_mapView.rotateEnabled = true
_mapView.delegate = self
self.view.addSubview(_mapView)
self.locateToLatitudeAndLongtitude(22.55088562, longti: 113.9663327)
}
func initSubViews()
{
let scrennWidth = UIScreen.mainScreen().bounds.size.width
var scrennheight = UIScreen.mainScreen().bounds.size.height
startField = UITextField(frame: CGRectMake((scrennWidth? - 200) / 2 , 30, 200, 25))
deretField = UITextField(frame: CGRectMake((scrennWidth? - 200) / 2 , 60, 200, 25))
startField.borderStyle = UITextBorderStyle.RoundedRect
deretField.borderStyle = UITextBorderStyle.RoundedRect
startField.becomeFirstResponder()
startField.placeholder = "輸入出發(fā)地"
deretField.placeholder = "輸入目的地"
startField.font = UIFont.systemFontOfSize(13)
deretField.font = UIFont.systemFontOfSize(13)
self.view.addSubview(startField)
self.view.addSubview(deretField)
nav = UIButton(frame: CGRectMake(startField.frame.origin.x + startField.frame.size.width + 10, 45, 60, 20))
nav.layer.borderWidth = 0.5
nav.layer.borderColor = UIColor.blackColor().CGColor
nav.layer.cornerRadius = 4.0
nav.titleLabel?.font = UIFont.systemFontOfSize(13)
nav.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
nav.setTitle("開(kāi)始導(dǎo)航", forState: UIControlState.Normal)
nav.addTarget(self, action: "startNavgation:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(nav)
}
func locateToLatitudeAndLongtitude(lat:Double , longti:Double)
{
let coor:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: lat, longitude: longti)
let sp:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
let re:MKCoordinateRegion = MKCoordinateRegion(center: coor, span: sp)
_mapView.setRegion(re, animated: true)
}
func startNavgation(senser:UIButton)
{
startField.resignFirstResponder()
deretField.resignFirstResponder()//失去第一響應(yīng)
if startField.text == nil || deretField.text == nil || startField.text == "" || deretField.text == ""
{
let aler:UIAlertView = UIAlertView(title: "提示", message: "出發(fā)地或者目的地?zé)o效", delegate: nil, cancelButtonTitle: "ok")
aler.show()
return
}
self.codeerWithString(startField.text!)//開(kāi)啟起始點(diǎn)編碼
}
func codeerWithString(address:NSString)
{
ge = CLGeocoder()//初始化
ge.geocodeAddressString(address as String) { ( placemarks:[CLPlacemark]?, error:NSError?) -> Void in
if? placemarks!.count > 0? //實(shí)際操作中 這里需要篩選處理
{
var pl:CLPlacemark = placemarks![0] as! CLPlacemark
if address == self.startField.text
{
self._startLocation = pl.location! as CLLocation //保存好編碼后的位置信息
var an:MKPointAnnotation = MKPointAnnotation()
an.coordinate = pl.location!.coordinate
an.title = "出發(fā)點(diǎn)"
self._mapView.addAnnotation(an)//添加大頭針
self.startMark = pl //placeMark信息保留
self.locateToLatitudeAndLongtitude(self._startLocation.coordinate.latitude, longti: self._startLocation.coordinate.longitude)
self.codeerWithString(self.deretField.text!)//開(kāi)啟目的地位置編碼
}else
{
self._dereLocation = pl.location! as CLLocation //保存好編碼后的位置信息
var an:MKPointAnnotation = MKPointAnnotation()
an.coordinate = pl.location!.coordinate
an.title = "目的地"
self._mapView.addAnnotation(an)//添加大頭針
/**************走到這里 位置信息編碼結(jié)束****************/
self.startNavagationWithMark(pl)//啟動(dòng)導(dǎo)航
}
}
}
}
//? ? func startNavagationWithMark(markL:CLPlacemark)
//? ? {
//
//? ? ? ? _mapView.removeOverlay(self.NavPath)//移除上次定位線路
//這個(gè)不能放在這里挂脑,應(yīng)該刪掉否則線程報(bào)錯(cuò)藕漱,私人認(rèn)為是你剛啟動(dòng)應(yīng)用的適合,沒(méi)有上次定位路線崭闲,由于在這里沒(méi)有用coredata什么的肋联,后臺(tái)走了,就自動(dòng)還原成剛開(kāi)始刁俭,沒(méi)有存儲(chǔ)數(shù)據(jù)橄仍。(我直接刪掉,成功啦牍戚。)
//
//? ? ? ? var request:MKDirectionsRequest = MKDirectionsRequest()//初始化一個(gè)導(dǎo)航請(qǐng)求
//
//
//
//? ? ? ? request.setSource = self.getAItemWithMark(startMark)//設(shè)置源頭.出發(fā)點(diǎn)
//
//
//? ? ? ? //設(shè)置目的地
//? ? ? ? request.setDestination = self.getAItemWithMark(markL)
//? ? ? ? //通過(guò)request 創(chuàng)建一個(gè)響應(yīng)頭
//? ? ? ? var mkdirection:MKDirections = MKDirections(request: request)
//
//? ? ? ? mkdirection.calculateDirectionsWithCompletionHandler { (response:MKDirectionsResponse!, err:NSError!) -> Void in
//
//? ? ? ? ? ? //查詢返回的第一條路線
//? ? ? ? ? ? var route:MKRoute = response.routes[0] as MKRoute
//? ? ? ? ? ? //保存路線
//? ? ? ? ? ? self.NavPath = route.polyline
//? ? ? ? ? ? //添加到地圖
//? ? ? ? ? ? self._mapView.addOverlay(self.NavPath, level: MKOverlayLevel.AboveLabels)
//
//
//? ? ? ? }
//
//? ? }
func startNavagationWithMark(markL:CLPlacemark){
let request:MKDirectionsRequest = MKDirectionsRequest() //初始化一個(gè)導(dǎo)航請(qǐng)求
//? ? ? ? self.getAItemWithMark(startMark)
//? ? ? ? request.setSource(self.getAItemWithMark(startMark))
//上面就是原來(lái)的侮繁,下面是我改得版本,我進(jìn)入到類里面看了如孝,原來(lái)是已經(jīng)取消了set,get方法宪哩,就直接用.賦值就行
request.source = self.getAItemWithMark(startMark)//設(shè)置源頭,出發(fā)點(diǎn)
request.destination = self.getAItemWithMark(markL)? //設(shè)置目的地
let mkdirection:MKDirections = MKDirections(request: request)//通過(guò)request創(chuàng)建一個(gè)響應(yīng)頭
mkdirection.calculateDirectionsWithCompletionHandler { (response:MKDirectionsResponse?, err:NSError?) -> Void in
//查詢返回的第一條路線
var route:MKRoute = (response?.routes[0])! as MKRoute
//保存路線
self.NavPath = route.polyline
self._mapView.addOverlay(self.NavPath, level: MKOverlayLevel.AboveLabels)
}
//? ? ? ? ? ? ? ? _mapView.removeOverlay(self.NavPath)//移除上一次定位的線路,剛開(kāi)始的時(shí)候第晰,锁孟,木有。
}
func getAItemWithMark(mark:CLPlacemark) -> MKMapItem
{
//通過(guò)CLPlaceMark創(chuàng)建一個(gè)MKPlaceMark
let mkp:MKPlacemark = MKPlacemark(placemark: mark)
//通過(guò)MKP 創(chuàng)建 MKtem
let item:MKMapItem = MKMapItem(placemark: mkp)
return item
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
//創(chuàng)建一個(gè)render
let naRende:MKPolylineRenderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
//設(shè)置路線圖線條顏色
naRende.strokeColor = UIColor.redColor()
naRende.lineWidth = 5.0
return naRende
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! {
let identy:NSString = "identy"
var pinView:MKPinAnnotationView? = mapView.dequeueReusableAnnotationViewWithIdentifier(identy as String) as? MKPinAnnotationView
if? nil == pinView
{
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identy as String)
}
if? annotation.title! == "出發(fā)點(diǎn)"
{
//大頭針的顏色為綠色
pinView?.pinColor = MKPinAnnotationColor.Green
}
else
{
//大頭針的顏色為紅色
pinView?.pinColor = MKPinAnnotationColor.Red
}
return pinView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
也就幾個(gè)小錯(cuò)而已茁瘦,我承認(rèn)這些錯(cuò)誤非常白癡品抽,但是。我反正很開(kāi)心自己解決bug啦甜熔。圆恤。