关于麦子学院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啦。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 概览 缓存组件应该说是每个客户端程序必备的核心组件,试想对于每个界面的访问都必须重新请求势必降低用户体验。但是如何...
    默默_David阅读 1,899评论 1 9
  • 定位相信对于好多app都很有用处,下面来讲解下在Swift3下GPS定位的实现,相对于之前的: 1.首先导入我们需...
    Miaoz0070阅读 2,593评论 0 5
  • 《失恋33天》:为什么偏偏是文章举荐你去演黄小仙?你是和他说好了么?为什么现实中剧情反转却殊途同归? 《与青春有关...
    大笨笨熊熊阅读 197评论 3 4
  • 一、我在60多岁的时候是什么样子? 这个60岁可能还有实现不了的可能,就先想65岁时的样子吧。 已经懂得欣赏所有存...
    在云之巅阅读 193评论 1 3
  • 2017,愿你我有光阴可虚度,有岁月可蹉跎,有美景可流连。 愿有明媚的阳光照耀,有清新的空气呼吸,有清澈的流...
    山河一梦阅读 422评论 1 9