Google地图缩放

问题描述 投票:0回答:1

我正在尝试从lat long上的特定图钉上缩放Google地图。它工作正常,但我想在swift.in上放大图钉时更改图钉图像。我这样做是这样,但是在同一幅lat上放了2张图像。

func zoom(lat: Double, long : Double){
        CATransaction.begin()
        CATransaction.setValue(1, forKey: kCATransactionAnimationDuration)

        // It will animate your camera to the specified lat and long
        let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 15)
        self.mapView!.animate(to: camera)
        let position = CLLocationCoordinate2D(latitude: lat,longitude: long)
        let marker = GMSMarker()
        marker.map = self.mapView
        marker.icon = UIImage.init(named: "pin-1")

         CATransaction.commit()
    }
swift google-maps dictionary colors zoom
1个回答
0
投票

您可以放置​​一个功能来检测缩放级别的变化,然后放置一个条件来更改标记图标的值。

这是我的代码的样子:

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {
 let marker = GMSMarker()

    override func viewDidLoad() {

    }

   override func loadView() {
    // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        self.view = mapView

    mapView.delegate = self

        // Creates a marker in the coordinate of the map.

        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
       marker.icon = UIImage(named: "pin_orange")
    }

//This detect the changes in the cameraposition
    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
          let zoom = mapView.camera.zoom
          print("map zoom is ",String(zoom))

        //put a condition here to change the icon of your marker
        if zoom > 6 {
            marker.icon = UIImage(named: "icon1")
        }else{
            marker.icon = UIImage(named: "icon2")
        }
    }
}

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.