ios - 用箭头替换 GMSMapView 中的蓝点 - Swift

标签 ios google-maps swift cllocation gmsmapview

我将 Google SDK 用于 iOS8 (Swift) 应用程序。 我想用根据智能手机方向旋转的箭头替换 GMSMapView 中的蓝点(我的位置)。 我怎样才能做到这一点?

编辑:

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

enter image description here

1:箭头被切断,我没有发现放大区域

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

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

3:箭头方向与原来对称我通过在“度”前添加“-”来解决这个问题:

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

如何解决这些问题?

最佳答案

如果您使用的是 Google Maps iOS SDK,似乎有 no way to replace默认用户位置图标。然而,默认图标已经有一个箭头来指示用户要去的方向(只需调用mapView.myLocationEnabled = true)。

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

示例代码(the full source code):

    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

enter image description here

关于ios - 用箭头替换 GMSMapView 中的蓝点 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676150/

相关文章:

ios - 外设管理器如何理解哪个是 CBCentral(设备)试图访问服务/特征

ios - 下载iOS应用程序时是否必须使用SSL证书设置Application Center?

javascript - Google 地点,类型区域的严格限制

google-maps - 如何让 Google map 在 Google Chrome 浏览器中以英语(带有英文字母)显示外国国家?

ios - UIBezierPath + CAShapeLayer - 制作一个充满圆圈的动画

swift - 保存表格 View 单元格

ios - 父子类之间的弱引用

ios - KeyboardWillShowNotification

ios - 有没有办法在 iOS 应用程序更新期间运行代码?

google-maps - 将 Google map /Mapquest 中的特定道路列入黑名单?