根据缩放级别缩放自定义标记图标

时间:2015-10-27 22:38:45

标签: ios swift google-maps-sdk-ios

我尝试根据缩放级别缩放自定义标记图标。当你缩放非常接近时,我的图标应该相当大,但显然当你向远处缩放时它会变大,所以它应该自动缩小。

我的最新方法如​​下:

var myscale = CGFloat()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    mapView.delegate = self

    mapView.myLocationEnabled = true

    if let mylocation = mapView.myLocation {
        print(mylocation)
        // use for camera position later on
    }
    else {
        // do sth else
    }

    let camera = GMSCameraPosition.cameraWithLatitude(47.962433, longitude: 12.526859, zoom: 17)
    mapView.camera = camera

    let userImage = UIImage(named: "test_profile_pic.png")!
    let userIcon = UIImage(data: UIImagePNGRepresentation(userImage)!, scale: myscale)

    let marker = GMSMarker()
    marker.icon = userIcon
    marker.position = CLLocationCoordinate2DMake(45.962433, 13.526859)
    marker.title = "testMarker"
    marker.snippet = "testSnippet"
    marker.map = mapView

    segmentedControl.layer.cornerRadius = 5
    segmentedControl.layer.masksToBounds = true

}
func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
    if (mapView.camera.zoom <= 16) {
        myscale = 4.0
    }
    // as soon is this works, some fine tuning in scaling
}

使用这种方法,图像不会重新缩放到我在didChangeCameraPosition中设置的值,因为它已经加载了比例1.0。我真的不知道如何更新我的UIImage的规模。任何帮助赞赏。

1 个答案:

答案 0 :(得分:0)

你必须先清除地图,然后重新添加标记!