2016-07-17 1 views
0

私は迅速であり、 私はユーザーの位置を表示し、その位置にマーカーを配置するアプリケーションを作成しています。ユーザーが移動した後マーカーは削除され、新しいマーカーが作成されます。今。私はポイントAとポイントBのマーカーをアプリにつけて地図上にルートを表示したい。地図上で最も近い道路を使用するものとする。 私は既にGoogleマップのドキュメントを研究していますが、私は2点間のルートを作る方法を理解できないのですか?スクロールでパスを取得するにはgoogle direction apiへのリクエストを送信する方法

ご協力いただければ幸いです、ありがとうございます。

答えて

0

blogから、という名前のプロパティを追加してインスタンス化するプロパティlet locationManager = CLLocationManager()を最初に追加する必要があります。

次に、viewDidLoad()を見つけ、の代理人をlocationManagerにしてユーザーの所在地へのアクセスを要求するこれらの2行を下に追加します。

この関連 threadから
locationManager.delegate = self 
locationManager.requestWhenInUseAuthorization() 

、あなたがこのような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に、positionを含むGMSMarkerオブジェクトを作成します。 titleとし、mapと設定します。次の例は、既存のGMSMapViewオブジェクトにマーカーを追加する方法を示しています。マーカーは座標10,10に作成され、クリックすると情報ウィンドウに「Hello world」という文字列が表示されます。

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

最後に、2点間の経路を作る、あなたはこれらのリンクをチェックできます。その都市の名前や、これまで何をしたいを与える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() 自己。displayRouteInfo() }他 { のprintln(ステータス) } }) }

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; 

希望に応じてパス を描画します!

関連する問題