Location and Mapping

作者: Girl_iOS | 来源:发表于2015-10-30 23:42 被阅读520次

    iOS9在MapKit和Core Location中增加了许多新特性.
    主要有几个方面:

    • 应用内地图新的点击弹出页面.
    • 苹果地图中几种交通工具的线路图.
    • 预估乘坐交通工具到达所需的时间.
    • Core Location利用Single location updates来定位.
      首先iOS9中的地图中可以显示罗盘 比例尺交通状况
    mapshow.png

    显示比例尺的方法很简单:

    mapView.showsScale = true
    

    以前pinColor颜色只有红、绿和紫色.iOS9中用pinTintColor来替代pinColor从而可以自定义颜色:

    annotationView!.pinTintColor = UIColor(red:0.5,  green:0.88, blue:0, alpha:1)
    

    以前我们定制一个点击大头针的弹出界面需要将你定制的界面添加到annotation View,而iOS9使之变得非常简单:MKAnnotationView增加了detailCalloutAccessoryView的属性,使定制页面更加方便,我们用Xib定义一个CoffeeShopPinDetailView.xib页面:

    mapxibview.png
    添加时只需:
    let detailView = UIView.loadFromNibNamed(identifier) as! CoffeeShopPinDetailView
    detailView.coffeeShop = annotation.coffeeshop
    annotationView!.detailCalloutAccessoryView = detailView
    

    即完成了自定义界面的添加:

    showview.png
    iOS9以前我们要定位位置需要创建CLLocationManager并实现代理方法且调用startUpdatingLocation(),当你获得位置后你需要调用stopUpdatingLocation()来停止更新位置.如果你不调用stop,就会一直获取位置信息,超费电.而iOS将其进化为只要调用一个方法requestLocation()即可.一旦获取你的位置后,只会调用一次代理方法,达到省电效果.实现代码:
      lazy var locationManager = CLLocationManager()
    
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    
        mapView.showsUserLocation = true 
        if CLLocationManager.authorizationStatus() ==
          .AuthorizedWhenInUse { 
            locationManager.requestLocation()   
        } else {
          locationManager.requestWhenInUseAuthorization()   
        }
    
    // MARK:- CLLocationManagerDelegate
    extension ViewController: CLLocationManagerDelegate {
     func locationManager(manager: CLLocationManager,didChangeAuthorizationStatus status: CLAuthorizationStatus) {
     if (status == CLAuthorizationStatus.AuthorizedAlways || status == CLAuthorizationStatus.AuthorizedWhenInUse) {
     locationManager.requestLocation() }
     } 
    func locationManager(manager: CLLocationManager,
     didUpdateLocations locations: [CLLocation]) { currentUserLocation = locations.first?.coordinate
     } 
    func locationManager(manager: CLLocationManager,
     didFailWithError error: NSError) {
     print("Error finding location:  + \(error.localizedDescription)")
    }
    }
    

    现在可以画出位置之间的交通连接,方法如下:

      func openTransitDirectionsForCoordinates(coord:CLLocationCoordinate2D) {
        let placemark = MKPlacemark(coordinate: coord,
          addressDictionary: coffeeShop.addressDictionary) 
        let mapItem = MKMapItem(placemark: placemark)  
        let launchOptions = [MKLaunchOptionsDirectionsModeKey:
          MKLaunchOptionsDirectionsModeTransit]  
        mapItem.openInMapsWithLaunchOptions(launchOptions)  
      }
    

    效果如下:

    transitmap.png
    我们可以计算出两个地点之间所花费的大概时间:
      func requestTransitTimes() {
        guard let currentUserLocation = currentUserLocation else {
          return
        }
        let request = MKDirectionsRequest()
        let source = MKMapItem(placemark:
          MKPlacemark(coordinate: currentUserLocation,
            addressDictionary: nil))
        let destination = MKMapItem(placemark:
          MKPlacemark(coordinate: coffeeShop.location,
            addressDictionary: nil))
        request.source = source
        request.destination = destination
        request.transportType = MKDirectionsTransportType.Transit
        let directions = MKDirections(request: request)
        directions.calculateETAWithCompletionHandler { response, error in
          if let error = error {
            print(error.localizedDescription)
          } else {
            self.updateEstimatedTimeLabels(response)
          }
        }
      }
    

    首先创建一个MKDirectionsRequest.然后创建你的当前位置和所要到达的位置MKMapItem,继而设置MKDirectionsRequest的source、destination和transportType.创建MKDirections来回调出时间.
    Girl学iOS100天 第3天 :)

    相关文章

      网友评论

        本文标题:Location and Mapping

        本文链接:https://www.haomeiwen.com/subject/kabfhttx.html