关于麦子学院ios地理位置及定位系统最后一个导航应用的(ios8->9)修改

我先废话一下下,一代学渣君。最近要期末啦。木有时间学习ios啦。

import UIKit

import MapKit

class ViewController: UIViewController ,MKMapViewDelegate {

var startField:UITextField!//出发地

var deretField:UITextField!//目的地

var nav:UIButton!//导航开启

var _mapView:MKMapView!

var _startLocation:CLLocation!//出发点位置信息

var _dereLocation:CLLocation!//目的地位置信息

var ge:CLGeocoder!//反编码管理类

var NavPath:MKPolyline! //导航线路

var startMark:CLPlacemark! //出发点的相关信息

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 = "输入出发地"

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("开始导航", 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()//失去第一响应

if startField.text == nil || deretField.text == nil || startField.text == "" || deretField.text == ""

{

let aler:UIAlertView = UIAlertView(title: "提示", message: "出发地或者目的地无效", delegate: nil, cancelButtonTitle: "ok")

aler.show()

return

}

self.codeerWithString(startField.text!)//开启起始点编码

}

func codeerWithString(address:NSString)

{

ge = CLGeocoder()//初始化

ge.geocodeAddressString(address as String) { ( placemarks:[CLPlacemark]?, error:NSError?) -> Void in

if  placemarks!.count > 0  //实际操作中 这里需要筛选处理

{

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 = "出发点"

self._mapView.addAnnotation(an)//添加大头针

self.startMark = pl //placeMark信息保留

self.locateToLatitudeAndLongtitude(self._startLocation.coordinate.latitude, longti: self._startLocation.coordinate.longitude)

self.codeerWithString(self.deretField.text!)//开启目的地位置编码

}else

{

self._dereLocation = pl.location! as CLLocation //保存好编码后的位置信息

var an:MKPointAnnotation = MKPointAnnotation()

an.coordinate = pl.location!.coordinate

an.title = "目的地"

self._mapView.addAnnotation(an)//添加大头针

/**************走到这里 位置信息编码结束****************/

self.startNavagationWithMark(pl)//启动导航

}

}

}

}

//    func startNavagationWithMark(markL:CLPlacemark)

//    {

//

//        _mapView.removeOverlay(self.NavPath)//移除上次定位线路

//这个不能放在这里,应该删掉否则线程报错,私人认为是你刚启动应用的适合,没有上次定位路线,由于在这里没有用coredata什么的,后台走了,就自动还原成刚开始,没有存储数据。(我直接删掉,成功啦。)

//

//        var request:MKDirectionsRequest = MKDirectionsRequest()//初始化一个导航请求

//

//

//

//        request.setSource = self.getAItemWithMark(startMark)//设置源头.出发点

//

//

//        //设置目的地

//        request.setDestination = self.getAItemWithMark(markL)

//        //通过request 创建一个响应头

//        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() //初始化一个导航请求

//        self.getAItemWithMark(startMark)

//        request.setSource(self.getAItemWithMark(startMark))

//上面就是原来的,下面是我改得版本,我进入到类里面看了,原来是已经取消了set,get方法,就直接用.赋值就行

request.source = self.getAItemWithMark(startMark)//设置源头,出发点

request.destination = self.getAItemWithMark(markL)  //设置目的地

let mkdirection:MKDirections = MKDirections(request: 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)

}

//                _mapView.removeOverlay(self.NavPath)//移除上一次定位的线路,刚开始的时候,,木有。

}

func getAItemWithMark(mark:CLPlacemark) -> MKMapItem

{

//通过CLPlaceMark创建一个MKPlaceMark

let mkp:MKPlacemark = MKPlacemark(placemark: mark)

//通过MKP 创建 MKtem

let item:MKMapItem = MKMapItem(placemark: mkp)

return item

}

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {

//创建一个render

let naRende:MKPolylineRenderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)

//设置路线图线条颜色

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! == "出发点"

{

//大头针的颜色为绿色

pinView?.pinColor = MKPinAnnotationColor.Green

}

else

{

//大头针的颜色为红色

pinView?.pinColor = MKPinAnnotationColor.Red

}

return pinView

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

也就几个小错而已,我承认这些错误非常白痴,但是。我反正很开心自己解决bug啦。。

你可能感兴趣的:(关于麦子学院ios地理位置及定位系统最后一个导航应用的(ios8->9)修改)