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;
希望に応じてパス を描画します!