基于此blog,您需要首先添加属性let locationManager = CLLocationManager(),该属性将添加并实例化名为locationManager 的CLLocationManager 属性。
接下来,找到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,创建一个包含position 和title 的GMSMarker 对象,并设置其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;
希望这会有所帮助!