gpt4 book ai didi

ios - 如何向 google Direction api 发送请求以快速获取路径

转载 作者:行者123 更新时间:2023-11-30 13:14:08 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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 ,创建一个包含位置标题GMSMarker对象,并设置其 map 。以下示例演示如何向现有 GMSMapView 对象添加标记。该标记在坐标 10,10 处创建,单击时会在信息窗口中显示字符串“Hello world”。

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

最后,在两点之间制定一条路线,您可以查看以下链接:

Now in createRoute method give that city name or what ever you want as origin like this:

@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: {

(status, success) -> Void in if success { self.configureMapAndMarkersForRoute() self.drawRoute() self.displayRouteInfo() } else { println(status) } }) }

    let closeAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) { (alertAction) -> Void in

}

addressAlert.addAction(createRouteAction)
addressAlert.addAction(closeAction)

presentViewController(addressAlert, animated: true, completion: nil)
}

First, get all points coordinates which are coming in route then add these points latitude and longitude in path in will draw path according to that

     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;

希望这有帮助!

关于ios - 如何向 google Direction api 发送请求以快速获取路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38421374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com