您好我试图将地图放在子视图中,但是当我在子视图中放置谷歌地图时它不起作用标记并且GPS坐标不起作用
-SWIFT CODE
import UIKit
import GoogleMaps
class HomeViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
let camera = GMSCameraPosition.cameraWithLatitude(15.4989, longitude: 73.8278, zoom: 6)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
// self.view = mapView
self.view.addSubview(mapView)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(15.4989, 73.8278)
marker.title = "Panjim"
marker.snippet = "Near Don Bosco,Alphran Plaza"
marker.map?.addSubview(mapView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
提前致谢
答案 0 :(得分:4)
我找到了解决方案。问题是:我正在创建一个新地图,然后在新地图上添加一个标记。然后用新地图我什么也没做。 所以这是我的解决方案:
@IBOutlet weak var subviewMap: GMSMapView!
func loadMap() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
subviewMap.camera = camera
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = subviewMap
}
它有效。
注意:不要忘记将您的子视图提及为IB中的GMSMapView类
感谢@ O-mkar和@mixth的努力。
快乐编码:]
答案 1 :(得分:3)
以下是添加标记的解决方案
makeprgBuild()
将相机设置为位置
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lat, long)
marker.appearAnimation = kGMSMarkerAnimationPop
marker.title = "Marker" // Marker title here
marker.snippet = "Tap the ↱ Navigate button to start navigating."
marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0)
marker.icon = UIImage(named: "marker") //Set marker icon here
marker.map = self.mapView // Mapview here
答案 2 :(得分:1)
我在另一个UIView里面有我的GMSMapView,一切正常。唯一不同的一行是:
marker.map = mapView
答案 3 :(得分:0)
after adding marker you should add some delay with this approach i have added 2 marker with bounds
DispatchQueue.main.async {
if self.markerArray.count > 1 {
var bounds = GMSCoordinateBounds()
for marker in self.markerArray {
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
}
self.isMovedTheMap = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.9, execute: {
self.superTopView.fadeOut()
let update = GMSCameraUpdate.fit(bounds, withPadding: 80)
self.mapView.animate(with: update)
})
}
}