繁体   English   中英

如何使用纬度和经度在 ARKit 中绘制折线?

[英]How can I draw a polyline in ARKit using latitude and longitude?

目前我可以在 ARKit Geolocation Tracking 中使用纬度和经度渲染球体,谁能指导我如何在 ARKit 中的 2 CLLocation 之间绘制折线。

这是在两点之间创建折线并设置该折线的宽度和颜色的完整代码

var locManager = CLLocationManager()
var currentLocation: CLLocation!

let annotation = MKPointAnnotation()
let annotation2 = MKPointAnnotation()

// MARK:- DRIVER -
var driverLatitute:String!
var driverLongitude:String!

// MARK:- RESTAURANT -
var restaurantLatitude:String!
var restaurantLongitude:String!

在视图中加载

// MARK:- 1 ( MAP ) -
        self.locManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            self.locManager.delegate = self
            self.locManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            self.locManager.startUpdatingLocation()
            print("UPDATE UPDATE")
        }
       
        if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
            print("")
        }

委托方法

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
              
        
                            //print("**********************")
                            //print("Long \(manager.location!.coordinate.longitude)")
                            //print("Lati \(manager.location!.coordinate.latitude)")
                            //print("Alt \(manager.location!.altitude)")
                            //print("Speed \(manager.location!.speed)")
                            //print("Accu \(manager.location!.horizontalAccuracy)")
                            //print("**********************")
                
                //print(Double((vendorLatitute as NSString).doubleValue))
                //print(Double((vendorLongitute as NSString).doubleValue))
                
        /*
         // restaurant
         self.restaurantLatitude     = (dict["deliveryLat"] as! String)
         self.restaurantLongitude    = (dict["deliveryLong"] as! String)
           
         // driver
         self.driverLatitute     = (dict["resturentLatitude"] as! String)
         self.driverLongitude    = (dict["resturentLongitude"] as! String)
         */
        
        let restaurantLatitudeDouble    = Double(self.restaurantLatitude)
        let restaurantLongitudeDouble   = Double(self.restaurantLongitude)
        let driverLatitudeDouble        = Double("\(manager.location!.coordinate.latitude)") //Double(self.driverLatitute)
        let driverLongitudeDouble       = Double("\(manager.location!.coordinate.longitude)") // Double(self.driverLongitude)
        
        let coordinate₀ = CLLocation(latitude: restaurantLatitudeDouble!, longitude: restaurantLongitudeDouble!)
            let coordinate₁ = CLLocation(latitude: driverLatitudeDouble!, longitude: driverLongitudeDouble!)
        
    /************************************** RESTAURANT LATITUTDE AND LONGITUDE  ********************************/
                        // first location
        let sourceLocation = CLLocationCoordinate2D(latitude: restaurantLatitudeDouble!, longitude: restaurantLongitudeDouble!)
    /********************************************************************************************************************/
                                            

    /************************************* DRIVER LATITUTDE AND LINGITUDE ******************************************/
                        // second location
                        let destinationLocation = CLLocationCoordinate2D(latitude: driverLatitudeDouble!, longitude: driverLongitudeDouble!)
    /********************************************************************************************************************/
          
                //print(sourceLocation)
                //print(destinationLocation)
                 
                        let sourcePin = customPin(pinTitle: "You", pinSubTitle: "", location: sourceLocation)
                        let destinationPin = customPin(pinTitle: "Driver", pinSubTitle: "", location: destinationLocation)

    /***************** REMOVE PREVIUOS ANNOTATION TO GENERATE NEW ANNOTATION *******************************************/
                    self.mapView.removeAnnotations(self.mapView.annotations)
    /********************************************************************************************************************/
                
        self.mapView.addAnnotation(sourcePin)
        self.mapView.addAnnotation(destinationPin)
                       
        let sourcePlaceMark = MKPlacemark(coordinate: sourceLocation)
        let destinationPlaceMark = MKPlacemark(coordinate: destinationLocation)
                                                   
        let directionRequest = MKDirections.Request()
        directionRequest.source = MKMapItem(placemark: sourcePlaceMark)
        directionRequest.destination = MKMapItem(placemark: destinationPlaceMark)
        directionRequest.transportType = .automobile
                               
        let directions = MKDirections(request: directionRequest)
        directions.calculate { [self] (response, error) in
            guard let directionResonse = response else {
                if let error = error {
                    print("we have error getting directions==\(error.localizedDescription)")
                }
                return
            }
                                   
    /***************** REMOVE PREVIUOS POLYLINE TO GENERATE NEW POLYLINE *******************************/
            let overlays = self.mapView.overlays
            self.mapView.removeOverlays(overlays)
    /************************************************************************************/
    
                                
    /***************** GET DISTANCE BETWEEN TWO CORDINATES *******************************/
                             
                                let distanceInMeters = coordinate₀.distance(from: coordinate₁)
                                // print(distanceInMeters as Any)
                                
                                // remove decimal
                                let distanceFloat: Double = (distanceInMeters as Any as! Double)
                                // print(distanceFloat as Any)
                                // self.lblDistance.text = (String(format: "Distance : %.0f Miles away", distanceFloat/1609.344))
                                self.lblTotalDistance.text = (String(format: "Distance : %.0f Miles away", distanceFloat/1609.344))
                                
                                // print(distanceFloat/1609.344)
                                // print(String(format: "Distance : %.0f Miles away", distanceFloat/1609.344))
                                
                                
                                let s:String = String(format: "%.0f",distanceFloat/1609.344)
                                // print(s as Any)
                               
                                 
                                  
                                
                                
                                
                                
                                
    /************************************************************************************/
                                
    /***************** GENERATE NEW POLYLINE *******************************/
                                let route = directionResonse.routes[0]
                                self.mapView.addOverlay(route.polyline, level: .aboveRoads)
                                let rect = route.polyline.boundingMapRect
                                self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)
    /************************************************************************************/
                           
                               }
        self.mapView.delegate = self
        print("update location after 5 sec")
        
        
        
                        // self.locManager.stopUpdatingLocation()
                
                // speed = distance / time
    }



    // line width of poly line
    //MARK:- MapKit delegates -
        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            let renderer = MKPolylineRenderer(overlay: overlay)
            renderer.strokeColor = UIColor.blue
            renderer.lineWidth = 4.0
            return renderer
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM