【问题标题】:how to send a request to google direction api to get a path in swift如何向谷歌方向api发送请求以快速获取路径
【发布时间】:2016-11-20 03:43:35
【问题描述】:

我是新来的 swift 和 我正在创建一个应用程序,它将显示用户的位置并在该位置放置一个标记。用户移动后。标记将被删除并创建一个新标记。现在。我想在 A 点和 B 点上做标记到应用程序中,并在地图上显示路线。它将使用地图上最近的道路。 我已经研究了谷歌地图文档,但我需要帮助我不明白如何在两点之间制作路线?

如果你能帮助我,我会很高兴,非常感谢。

【问题讨论】:

    标签: ios swift google-maps google-maps-api-3 google-directions-api


    【解决方案1】:

    基于此blog,您需要首先添加属性let locationManager = CLLocationManager(),该属性将添加并实例化名为locationManagerCLLocationManager 属性。

    接下来,找到viewDidLoad() 并将这两行添加到底部,这将使MapViewController 成为locationManager 的代表并请求访问用户的位置。

    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    

    从这个相关的thread,您必须像这样在viewDidLoad() 中实例化CLLocationManager 类:

    // Ask for Authorisation from the User.
    self.locationManager.requestAlwaysAuthorization() 
    
    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()
    
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    

    然后在CLLocationManagerDelegate方法中可以得到用户当前的位置坐标:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        var locValue:CLLocationCoordinate2D = manager.location.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
    

    然后到add a marker,创建一个包含positiontitleGMSMarker 对象,并设置其map。以下示例演示如何将标记添加到现有 GMSMapView 对象。标记在坐标10,10 处创建,并在单击时在信息窗口中显示字符串“Hello world”。

    let  position = CLLocationCoordinate2DMake(10, 10)
    let marker = GMSMarker(position: position)
    marker.title = "Hello World"
    marker.map = mapView
    

    最后,在两点之间做一条路线,你可以检查这些链接:

    现在在createRoute 方法中给出该城市名称或您想要的任何名称 像这样的起源:

    @IBAction func createRoute(sender: AnyObject) {
    
        let addressAlert = UIAlertController(title: "Create Route", message: "Connect locations with a route:", preferredStyle:
    

    UIAlertControllerStyle.Alert)

        addressAlert.addTextFieldWithConfigurationHandler { (textField) -> Void in
            //give a origin for route
            textField.text = self.currentLocationName
            textField.userInteractionEnabled = false
        }
    
        addressAlert.addTextFieldWithConfigurationHandler { (textField) -> Void in
            textField.placeholder = "Destination?"
        }
    
    
        let createRouteAction = UIAlertAction(title: "Create Route", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
            let origin = (addressAlert.textFields![0] as! UITextField).text as String
            let destination = (addressAlert.textFields![1] as! UITextField).text as String
    
            self.mapTasks.getDirections(origin, destination: destination, waypoints: nil, travelMode: nil, completionHandler: {
    

    (状态,成功)-> 无效 如果成功{ self.configureMapAndMarkersForRoute() self.drawRoute() self.displayRouteInfo() } 别的 { 打印(状态) } }) }

        let closeAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) { (alertAction) -> Void in
    
        }
    
        addressAlert.addAction(createRouteAction)
        addressAlert.addAction(closeAction)
    
        presentViewController(addressAlert, animated: true, completion: nil)
    }
    

    首先,获取路线中的所有点坐标,然后添加 这些点在路径中的纬度和经度将绘制路径 据此

         GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:18.5203 longitude:73.8567 zoom:12];
            _mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
            _mapView.myLocationEnabled=YES;
            GMSMarker *marker=[[GMSMarker alloc]init];
            marker.position=CLLocationCoordinate2DMake(18.5203, 73.8567);
            marker.icon=[UIImage imageNamed:@"aaa.png"] ;
            marker.groundAnchor=CGPointMake(0.5,0.5);
            marker.map=_mapView;
            GMSMutablePath *path = [GMSMutablePath path];   
            [path addCoordinate:CLLocationCoordinate2DMake(@(18.520).doubleValue,@(73.856).doubleValue)];
            [path addCoordinate:CLLocationCoordinate2DMake(@(16.7).doubleValue,@(73.8567).doubleValue)];
    
            GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
            rectangle.strokeWidth = 2.f;
            rectangle.map = _mapView;
            self.view=_mapView;
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      相关资源
      最近更新 更多