2017-02-26 53 views
0

我现在正在进行一个项目,并被卡在最后一部分。我设计了一个包含20个自定义注释的应用程序,供我们的婚礼客人使用,以查找附近的餐馆,酒吧,咖啡厅等。有各种自定义注释来标识每个位置。它还要求用户当前的位置,以便他们可以找到附近的东西。我希望能够点击注释并让我们的朋友和家人能够从他们的当前位置获得路线到注释。看起来最好的方法是在我的自定义点注释中打开苹果地图应用程序以及我的纬度和经度坐标。我的代码虽然似乎并没有打开地图应用程序。我已经添加了一个正确的呼叫,我仔细查看了每一个新老问题,并且无法找到解决方案。任何意见将不胜感激,因为这是我第一个真正的快速应用程序。以下是我目前的开放方向功能。提前感谢您的帮助或见解。打开方向func不能以编程方式打开苹果地图

func openDirections(_ address :String?) { 

    self.geocoder.geocodeAddressString(address!, completionHandler: { (placemarks :[CLPlacemark]?, error :NSError?) -> Void in 

     let placemark = placemarks![0] as CLPlacemark 

     let destinationPlacemark = MKPlacemark(coordinate: placemark.location!.coordinate, addressDictionary: placemark.addressDictionary as? [String:NSObject]) 

     let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: placemark.location!.coordinate, addressDictionary:nil)) 
     mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]) 

     let startingMapItem = MKMapItem.forCurrentLocation() 
     let destinationMapItem = MKMapItem(placemark: destinationPlacemark) 

     let directionsRequest = MKDirectionsRequest() 
     directionsRequest.transportType = .automobile 
     directionsRequest.source = startingMapItem 
     directionsRequest.destination = destinationMapItem 

     let directions = MKDirections(request: directionsRequest) 

     directions.calculate(completionHandler: { (response :MKDirectionsResponse?, error :NSError?) -> Void in 

      let route = response!.routes[0] as MKRoute 

      if route.steps.count > 0 { 

       for step in route.steps { 

        print(step.instructions) 

       } 
      } 

      self.mapView!.add((route.polyline), level: MKOverlayLevel.aboveRoads) 
      } as! MKDirectionsHandler) 
     } as! CLGeocodeCompletionHandler) 
} 
+0

为什么要打开地图应用程序?你有一个'MKMapView'的权利?您可以使用它并制作自定义注释。您可以使用'CoreLocation'框架来获取并更新当前用户的位置。我想我不明白为什么你会想要强制用户从你的应用程序去苹果地图应用程序,当它不是必要的。 – Pierce

+0

@Pierce我也会对这个选项感到满意,但是在搜索到关于SO的多个教程/问题之后,我们很难弄清楚。我似乎无法找到一个很好的解决方案,在具有多个注释的应用程序中执行此操作。任何有关解决这个问题的最佳方法的建议将不胜感激。 – Jacquedes

+0

你知道什么是要学习的东西,并且比我在评论或回答中可以告诉你的方式要多得多。看看这个免费的教程。它应该覆盖您可能需要的所有内容https://www.raywenderlich.com/136165/core-location-geofencing-tutorial – Pierce

回答

0

继续前进,删除您写下的所有内容,并对如何编写新的,更好,更清晰的函数进行一些研究。这是为我工作,我希望它可以帮助别人。

func mapView(_ mapView: MKMapView, annotationView view:MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 

    let placemark = MKPlacemark(coordinate: view.annotation!.coordinate, addressDictionary: nil) 
    let mapItem = MKMapItem(placemark: placemark) 
    let launchOptions = [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeTransit] 
    self.title = title 
    mapItem.name = title 
    mapItem.openInMaps(launchOptions: launchOptions) 
}