更改GoogleMaps地图类型

时间:2016-12-07 14:16:19

标签: ios swift google-maps maps

我有一个简单的代码,它提供了一个具有少量不同位置的地图,这些地图根据表格中的点击行而变化。地图显示为预期,但是当我尝试将类型更改为,比方说,卫星时,地图会淡出,但Google徽标仍然存在,我在地图上设置的标记也是如此。我无法弄清楚我在这里失踪了什么。这是一些代码。

@IBOutlet var myMapView: UIView!
var mapView: GMSMapView?
var currentLocation: CLLocationCoordinate2D?
let locationManager = CLLocationManager()
var marker = GMSMarker()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    kreirajMapu() -  creates the map
}

func kreirajMapu() { 
    currentLocation = magacin[red].artikal[redZaMapu].lokacija

    let camera = GMSCameraPosition.camera(withLatitude: (currentLocation?.latitude)!, longitude: (currentLocation?.longitude)!, zoom: 15)
    mapView = GMSMapView.map(withFrame: myMapView.bounds, camera: camera)
    mapView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    mapView?.isMyLocationEnabled = true
    mapView?.settings.compassButton = true
    mapView?.settings.myLocationButton = true
    mapView?.settings.setAllGesturesEnabled(true)
    myMapView.addSubview(mapView!)
}

func moveCamera() {

    CATransaction.begin()
    CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)

    mapView?.animate(to: GMSCameraPosition.camera(withTarget: magacin[red].artikal[redZaMapu].lokacija, zoom: magacin[red].artikal[redZaMapu].zoom))
    mapView?.animate(toViewingAngle: 45)

    CATransaction.commit()
    marker = GMSMarker(position: magacin[red].artikal[redZaMapu].lokacija)
    marker.title = magacin[red].artikal[redZaMapu].naziv
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.snippet  = "This is where \(magacin[red].artikal[redZaMapu].naziv)'s live"
    marker.map = mapView
}

@IBAction func mapType(_ sender: AnyObject) {
    let actionSheet = UIAlertController(title: "Map Types", message: "Select map type:", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Normal", style: .default, handler: {_ in
        self.mapView?.mapType = kGMSTypeNormal
    }))

    actionSheet.addAction(UIAlertAction(title: "Hybrid", style: .default, handler: {_ in
        self.mapView?.mapType = kGMSTypeHybrid
    }))

    actionSheet.addAction(UIAlertAction(title: "Satellite", style: .default, handler: {_ in
        self.mapView?.mapType = kGMSTypeSatellite
    }))

    actionSheet.addAction(UIAlertAction(title: "Terrain", style: .default, handler: {_ in
        self.mapView?.mapType = kGMSTypeTerrain
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    present(actionSheet, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

我的问题的答案是,确保在google.developer上为您尝试使用它的应用启用GoogleMap API。感谢大家的贡献:/