關(guān)于麥子學(xué)院ios地理位置及定位系統(tǒng)最后一個導(dǎo)航應(yīng)用的(ios8->9)修改

我先廢話一下下,一代學(xué)渣君。最近要期末啦。木有時間學(xué)習(xí)ios啦。

import UIKit

import MapKit

class ViewController: UIViewController ,MKMapViewDelegate {

var startField:UITextField!//出發(fā)地

var deretField:UITextField!//目的地

var nav:UIButton!//導(dǎo)航開啟

var _mapView:MKMapView!

var _startLocation:CLLocation!//出發(fā)點位置信息

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

var ge:CLGeocoder!//反編碼管理類

var NavPath:MKPolyline! //導(dǎo)航線路

var startMark:CLPlacemark! //出發(fā)點的相關(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("開始導(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!)//開啟起始點編碼

}

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 = "出發(fā)點"

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)//添加大頭針

/**************走到這里 位置信息編碼結(jié)束****************/

self.startNavagationWithMark(pl)//啟動導(dǎo)航

}

}

}

}

//? ? func startNavagationWithMark(markL:CLPlacemark)

//? ? {

//

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

//這個不能放在這里,應(yīng)該刪掉否則線程報錯,私人認(rèn)為是你剛啟動應(yīng)用的適合,沒有上次定位路線,由于在這里沒有用coredata什么的,后臺走了,就自動還原成剛開始,沒有存儲數(shù)據(jù)。(我直接刪掉,成功啦。)

//

//? ? ? ? var request:MKDirectionsRequest = MKDirectionsRequest()//初始化一個導(dǎo)航請求

//

//

//

//? ? ? ? request.setSource = self.getAItemWithMark(startMark)//設(shè)置源頭.出發(fā)點

//

//

//? ? ? ? //設(shè)置目的地

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

//? ? ? ? //通過request 創(chuàng)建一個響應(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() //初始化一個導(dǎo)航請求

//? ? ? ? self.getAItemWithMark(startMark)

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

//上面就是原來的,下面是我改得版本,我進(jìn)入到類里面看了,原來是已經(jīng)取消了set,get方法,就直接用.賦值就行

request.source = self.getAItemWithMark(startMark)//設(shè)置源頭,出發(fā)點

request.destination = self.getAItemWithMark(markL)? //設(shè)置目的地

let mkdirection:MKDirections = MKDirections(request: request)//通過request創(chuàng)建一個響應(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)//移除上一次定位的線路,剛開始的時候,,木有。

}

func getAItemWithMark(mark:CLPlacemark) -> MKMapItem

{

//通過CLPlaceMark創(chuàng)建一個MKPlaceMark

let mkp:MKPlacemark = MKPlacemark(placemark: mark)

//通過MKP 創(chuàng)建 MKtem

let item:MKMapItem = MKMapItem(placemark: mkp)

return item

}

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

//創(chuàng)建一個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ā)點"

{

//大頭針的顏色為綠色

pinView?.pinColor = MKPinAnnotationColor.Green

}

else

{

//大頭針的顏色為紅色

pinView?.pinColor = MKPinAnnotationColor.Red

}

return pinView

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

也就幾個小錯而已,我承認(rèn)這些錯誤非常白癡,但是。我反正很開心自己解決bug啦。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概覽 緩存組件應(yīng)該說是每個客戶端程序必備的核心組件,試想對于每個界面的訪問都必須重新請求勢必降低用戶體驗。但是如何...
    默默_David閱讀 2,045評論 1 9
  • 定位相信對于好多app都很有用處,下面來講解下在Swift3下GPS定位的實現(xiàn),相對于之前的: 1.首先導(dǎo)入我們需...
    Miaoz0070閱讀 2,760評論 0 5
  • 《失戀33天》:為什么偏偏是文章舉薦你去演黃小仙?你是和他說好了么?為什么現(xiàn)實中劇情反轉(zhuǎn)卻殊途同歸? 《與青春有關(guān)...
    大笨笨熊熊閱讀 249評論 3 4
  • 一、我在60多歲的時候是什么樣子? 這個60歲可能還有實現(xiàn)不了的可能,就先想65歲時的樣子吧。 已經(jīng)懂得欣賞所有存...
    在云之巔閱讀 219評論 1 3
  • 2017,愿你我有光陰可虛度,有歲月可蹉跎,有美景可流連。 愿有明媚的陽光照耀,有清新的空氣呼吸,有清澈的流...
    山河一夢閱讀 488評論 1 9

友情鏈接更多精彩內(nèi)容