更改Google地图的地图类型
问题描述:
我有一个简单的代码,它显示了几个不同位置的地图,这些地图会根据表中点击的行进行更改。地图显示为预期的,但是当我尝试将类型更改为Satellite时,地图只是淡出,但Google徽标仍然存在,我在地图上设置的标记也一样。我无法弄清楚我在这里错过了什么。这里有一些代码。更改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)
}
答
我的问题的答案是,请确保在google.developer上启用谷歌地图API,以便您尝试使用它。感谢大家的贡献:/