用箭头replaceGMSMapView中的蓝点 – Swift

我使用Google SDK作为应用程序iOS8(Swift)。 我想用智能手机的方向旋转的箭头来replaceGMSMapView中的蓝点(我的位置)。 我怎样才能做到这一点?

编辑:

我设法看到我的箭头,但我有三个问题:

在这里输入图像说明

1:箭头被切断,我没有find放大的区域

2:箭头的位置不完全在我的位置我通过删除“0,5”来解决这个问题:

CGContextTranslateCTM(context, size.width, size.height) 

3:箭头的方向与原来的对称我在“度”之前加“ – ”来解决这个问题:

 CGContextRotateCTM(context, CGFloat(DegreesToRadians(Double(-degrees)))) 

如何解决这些问题?

如果您使用Google Maps iOS SDK,似乎无法replace默认的用户位置图标。 然而,默认的图标已经有一个箭头来指示用户正在前进的方向(只需调用mapView.myLocationEnabled = true )。

解决方法是在用户的当前位置放置一个标记,然后根据标题更改标记图像。

示例代码( 完整的源代码 ):

  func RadiansToDegrees(radians: Double) -> Double { return radians * 180.0/M_PI } func DegreesToRadians(degrees: Double) -> Double { return degrees * M_PI / 180.0 } var GeoAngle = 0.0 var locationManager = CLLocationManager() var marker = GMSMarker() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6) var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) self.view = mapView marker.map = mapView locationManager.delegate = self locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() locationManager.startUpdatingHeading() } func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) { var camera = GMSCameraPosition.cameraWithLatitude(newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude, zoom: 15) (self.view as GMSMapView).animateToCameraPosition(camera) GeoAngle = self.setLatLonForDistanceAndAngle(newLocation) marker.position = newLocation.coordinate } func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) { var direction = -newHeading.trueHeading as Double marker.icon = self.imageRotatedByDegrees(CGFloat(direction), image: UIImage(named: "arrow.png")!) } func imageRotatedByDegrees(degrees: CGFloat, image: UIImage) -> UIImage{ var size = image.size UIGraphicsBeginImageContext(size) var context = UIGraphicsGetCurrentContext() CGContextTranslateCTM(context, 0.5*size.width, 0.5*size.height) CGContextRotateCTM(context, CGFloat(DegreesToRadians(Double(degrees)))) image.drawInRect(CGRect(origin: CGPoint(x: -size.width*0.5, y: -size.height*0.5), size: size)) var newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } func setLatLonForDistanceAndAngle(userLocation: CLLocation) -> Double { var lat1 = DegreesToRadians(userLocation.coordinate.latitude) var lon1 = DegreesToRadians(userLocation.coordinate.longitude) var lat2 = DegreesToRadians(37.7833); var lon2 = DegreesToRadians(-122.4167); var dLon = lon2 - lon1; var y = sin(dLon) * cos(lat2); var x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon); var radiansBearing = atan2(y, x); if(radiansBearing < 0.0) { radiansBearing += 2*M_PI; } return radiansBearing; } 

关键的一步是根据来自func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!)方法的程度来改变图像。

您还需要确保在您的info.plist文件中添加NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

在这里输入图像说明

iOS中的Google地图的默认蓝色图标可以通过设置隐藏

 mapView.myLocationEnabled = FALSE;