swift mapkit显示路线并计算距离

时间:2017-09-02 11:33:45

标签: swift macos mapkit

我使用swift 4 for osx。 我想了解以下情况:

  • 显示地图(此作品)
  • 显示sourceLocation(这可行)
  • 显示targetLocation(此作品)
  • 获取sourceLocation和targetLocation之间的距离(这可行)

这是我的代码:

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {

    let myLocation = myMap.userLocation.coordinate
    myMap.setRegion(MKCoordinateRegionMakeWithDistance(myLocation, 500, 500), animated: true)

       let address = "XXX"
       let geoCoder = CLGeocoder()
       geoCoder.geocodeAddressString(address) { (placemarks, error) in
       guard let placemarks = placemarks, let targetLocation = placemarks.first?.location
       else {
          return
       }

       let sourceLocation = CLLocation(latitude: myLocation.latitude, longitude: myLocation.longitude)
       var distance = sourceLocation .distance(from: targetLocation)


       let annotation = MKPointAnnotation()
       annotation.coordinate = targetLocation.coordinate
       self.myMap.addAnnotation(annotation)


       // Distance in km between sourceLocation and targetLocation
       print(distance.rounded()/1000, " km")
      }              
    } 

现在我的问题:)

  • 每次启动我的应用程序并显示地图时,都会显示用户位置的身份验证请求。有没有办法保存用户的身份验证答案,以便每次都不会发出请求?
  • 有没有办法显示user-to targetLocation的可能路由。如果用户选择路由,该距离将保存到var距离?

0 个答案:

没有答案