Google マップ iOS SDK を使用している場合、デフォルトのユーザー位置アイコンを置き換える方法はないようです。ただし、デフォルトのアイコンには、ユーザーがどちらの方向に進んでいるかを示す矢印が既にあります (単に を呼び出します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!)
です。
また、必ずファイルにNSLocationAlwaysUsageDescription
andを追加する必要がありNSLocationWhenInUseUsageDescription
ますinfo.plist
。
