基于swift MKMapkit 开发的地图定位导航

 

//  DTOneViewController.swift

//  Mapper-JSON

//

//  Created by kcl on 16/8/8.

//  Copyright © 2016年 kcl. All rights reserved.

//

 

import UIKit

import MapKit

class DTOneViewController: UIViewController {

 

    var locationManager:CLLocationManager!

    var mapView:MKMapView!

 

    override func viewDidLoad() {

        super.viewDidLoad()

 

        self.locationManager = CLLocationManager()

        

        self.mapView = MKMapView()

        self.mapView.frame = self.view.bounds

        self.mapView.delegate = self

        self.mapView.userTrackingMode = .Follow

        self.mapView.showsUserLocation = true

        self.mapView.mapType = MKMapType.Standard

        self.view.addSubview(self.mapView)

        self.locationManager.requestWhenInUseAuthorization()

        self.locationManager.requestAlwaysAuthorization()

   

 

    }

 

    override func didReceiveMemoryWarning() {

        

        super.didReceiveMemoryWarning()

     

    }

    func goSearch(){

        let fromCoordinate = CLLocationCoordinate2D(latitude: 22.546036, longitude: 113.960423)

        let tofromCoordinate = CLLocationCoordinate2D(latitude: 22.588416, longitude: 113.972166)

        let fromPlaceMark = MKPlacemark(coordinate: fromCoordinate, addressDictionary: nil)

        let toPlaceMark = MKPlacemark(coordinate: tofromCoordinate, addressDictionary: nil)

        let fromItem = MKMapItem(placemark: fromPlaceMark)

        let toItem = MKMapItem(placemark: toPlaceMark)

        self.findDirectionsFrom(fromItem, destination: toItem)

    }

    

    func findDirectionsFrom(source:MKMapItem,destination:MKMapItem){

        let request = MKDirectionsRequest()

        request.source = source

        request.destination = destination

        request.transportType = MKDirectionsTransportType.Walking

        request.requestsAlternateRoutes = true;

        let directions = MKDirections(request: request)

//        j

        directions.calculateDirectionsWithCompletionHandler { (response, error) in

            if error == nil {

               self.showRoute(response!)

            }else{

                print("trace the error \(error?.localizedDescription)")

            }

        }

    }

    func showRoute(response:MKDirectionsResponse) {

        

        for route in response.routes {

            mapView.addOverlay(route.polyline,level: MKOverlayLevel.AboveRoads)

            let routeSeconds = route.expectedTravelTime

            let routeDistance = route.distance

            print("distance between two points is \(routeSeconds) and \(routeDistance)")

        }

    }

 

 

}

extension DTOneViewController:MKMapViewDelegate,CLLocationManagerDelegate {

    

    func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {

        

        let lat = 0.005

        let lon = 0.005

        let currentLocationSpan:MKCoordinateSpan = MKCoordinateSpanMake(lat, lon)

        let loc:CLLocationCoordinate2D = userLocation.coordinate

        let region:MKCoordinateRegion = MKCoordinateRegionMake(loc, currentLocationSpan)

        self.mapView.setRegion(region, animated: true)

        goSearch()

    }

    

    

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let currentlocation:CLLocation = locations.first!

        print("currentlocation = \(currentlocation.coordinate.latitude) = \(currentlocation.coordinate.longitude)")

 

    }

//    绚烂绘制polyLine

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

//        let render = MKPolygonRenderer(overlay: overlay)

//        render.strokeColor = UIColor.redColor()

//        render.lineWidth = 4.0

//        return render

//        if overlay is MKPolyline {

        let  polylineRenderer = MKPolylineRenderer(overlay: overlay)

//      polylineRenderer.lineDashPattern = [14,10,6,10,4,10]

        polylineRenderer.strokeColor = UIColor.redColor()

//      polylineRenderer.strokeColor = UIColor(red: 0.012, green: 0.012, blue: 0.012, alpha: 1.00)

        polylineRenderer.fillColor = UIColor.blueColor()

        polylineRenderer.lineWidth = 2.5

        return polylineRenderer

//        }

//        return nil

    }

}

posted @ 2016-08-09 10:52  独步武林  阅读(988)  评论(1编辑  收藏  举报