开发环境:Xcode6.4
模拟器 : IOS8.4
OS X : 10.0.4
小编博客链接: http://www.goofyy.com
首先例子是小编写的一个定位获取经纬度,然后在地图上面显示,并自定义大头针的一个程序。先上图
首先说一下定位,在 iOS 8 之前,位置服务的权限是二元的:你要么赋予一个应用得到使用位置服务的权限,要么不给。你可以在 Settings.app 查看哪些 app 可以在后台取得你的位置信息,但除了完全不让这个 app 使用位置服务之外,你不能做任何的事来阻止它获取位置信息,但是在IOS8之后, 修改了这个问题,ISO8把应用程序分成两个权限,
“使用期间” 正如标题,程序在使用期间会获取你的位置信息。 “始终” 这个就跟IOS8之前一样,会在程序中一直获取你的个人信息。
所以我们在使用地图的时候,要在Info.plist里面添加两行文字,设置我们程序使用地图的权限。
NSLocationWhenInUseUsageDescription //用的时候获取位置 NSLocationAlwaysUsageDescription //始终获取用户位置
首先就是先把CoreLocation框架引入过来。直接晒代码吧。懒得写了。代码下面注释
// // ViewController.swift // 获取路线 // // Created by goofygao on 8/18/15. // Copyright (c) 2015 goofyy. All rights reserved. // import UIKit import MapKit import CoreLocation class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate { @IBOutlet weak var mapView: MKMapView! var locationManger:CLLocationManager = CLLocationManager() var location:CLLocation! var ann:MKPointAnnotation = MKPointAnnotation() override func viewDidLoad() { super.viewDidLoad() locationManger.delegate = self locationManger.desiredAccuracy = kCLLocationAccuracyBest locationManger.distanceFilter = kCLLocationAccuracyKilometer locationManger.requestAlwaysAuthorization() locationManger.startUpdatingLocation() mapView.delegate = self mapView.mapType = MKMapType.Satellite ann.title = "定位的位置" ann.subtitle = "广州" mapView.addAnnotation(ann) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /** 重写 - 更改大头针颜色 :param: mapView :param: annotation :returns: MKAnnotationView */ func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { var view:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin") view.pinColor = MKPinAnnotationColor.Red //view.image = UIImage(named: "localtion.png") var viewLeft = UIView(frame: CGRectMake(0, 0, 50, 50)) viewLeft.backgroundColor = UIColor.redColor() var viewRight = UIView(frame: CGRectMake(0, 0, 50, 50)) viewRight.backgroundColor = UIColor.greenColor() view.leftCalloutAccessoryView = viewLeft view.rightCalloutAccessoryView = viewRight view.draggable = true view.canShowCallout = true return view } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { location = locations.last as! CLLocation if((location) == nil) { locationManger.startUpdatingLocation() } println("\(location.coordinate.longitude)") mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: MKCoordinateSpanMake(20, 20)) ann.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) } }
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { }
locationManger.delegate = self locationManger.desiredAccuracy = kCLLocationAccuracyBest locationManger.distanceFilter = kCLLocationAccuracyKilometer locationManger.requestAlwaysAuthorization() locationManger.startUpdatingLocation()
然后到了设置地图地方了。添加地图的代理,设置大头针的主标题和副标题。
mapView.delegate = self mapView.mapType = MKMapType.Satellite ann.title = "定位的位置" ann.subtitle = "广州" mapView.addAnnotation(ann)
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { var view:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin") view.pinColor = MKPinAnnotationColor.Red //view.image = UIImage(named: "localtion.png") var viewLeft = UIView(frame: CGRectMake(0, 0, 50, 50)) viewLeft.backgroundColor = UIColor.redColor() var viewRight = UIView(frame: CGRectMake(0, 0, 50, 50)) viewRight.backgroundColor = UIColor.greenColor() view.leftCalloutAccessoryView = viewLeft view.rightCalloutAccessoryView = viewRight view.draggable = true view.canShowCallout = true return view }
location = locations.last as! CLLocation if((location) == nil) { locationManger.startUpdatingLocation() } println("\(location.coordinate.longitude)") mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: MKCoordinateSpanMake(20, 20)) ann.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) }
然后看下一个。
地图覆盖层
简单来说就是在地图地图上面画图。
首先是在地图上面画线
var coor = [CLLocationCoordinate2D]() for (var i=0; i<5; i++) { po = CLLocationCoordinate2DMake(CLLocationDegrees(23 + i), 113) coor.append(po) } var line = MKPolyline(coordinates: &coor, count: 5) mapView.addOverlay(line)
这里有一个犯错点,var line =MKPolyline(coordinates: &coor, count:5) 。
MKPolyline(coordinates: <#UnsafeMutablePointer<CLLocationCoordinate2D>#>, count: <#Int#>)
然后添加 MKMapViewDelegate 代理,实现代理的方法。
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { var render = MKPolylineRenderer(overlay: overlay) render.lineWidth = 3 render.strokeColor = UIColor.redColor() return render }
完整项目 https://github.com/goooooooofy/originMapApple.git
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u014406672/article/details/47832695