Reputation: 31
I would appreciate some help for my problem, I am creating a view called mapView and have changed its class to GMSMapView. But when I assign the map to this mapView, the layout is perfectly fine but the camera is set to Europe by default, which does not change.
Following is the code:
@IBOutlet weak var mapView: GMSMapView!
var latitudes = [19.199782,19.19855,19.199179]
var longitudes = [72.8734634,72.872935, 72.874535]
var titles = ["Riviera","Alica","Senate"]
var subTitle = ["1","2","3"]
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 19.20060, longitude: 72.8734462, zoom: 15.0)
let mapView1 = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
let path = GMSMutablePath()
for i in 0...2
{
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
marker.title = titles[i]
marker.snippet = subTitle[i]
marker.map = mapView1
path.add(CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i]))
}
let polyLine = GMSPolyline(path: path)
polyLine.map = mapView1
mapView = mapView1
mapView.camera = camera
}
The output for the code is: Output
When I add the map to view I get the desired map
modification to above code: instead of mapView = mapView1 put view = mapView1
Desired output:Desired Output
So please help me getting the desired output in the mapView view of my screen. Thank You!
Upvotes: 2
Views: 1301
Reputation: 6347
The GMSMapView class has the following function:
animate(to:GMSCameraPosition)
So in your code, instead of setting the camera property on your mapview, do this:
mapView.animate(to: camera)
Hope this helps!
After looking more into this code, I believe your mapView1 is unnecessary and causing problems with your outlet. Try something like this:
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 19.20060, longitude: 72.8734462, zoom: 15.0)
let path = GMSMutablePath()
for i in 0...2
{
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
marker.title = titles[i]
marker.snippet = subTitle[i]
marker.map = mapView
path.add(CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i]))
}
let polyLine = GMSPolyline(path: path)
polyLine.strokeWidth = 2.0
polyLine.strokeColor = .black
polyLine.map = mapView
mapView.animate(to: camera)
}
On a side note I would also suggest replacing your four arrays with one array of dictionary objects each containing a title, subtitle, latitude and longitude, or something like that. Then change your for loop to accommodate
Upvotes: 1
Reputation: 1507
I would suggest setting view's class to UIView, since you create the mapView1 programmatically and then after initializing mapView1
let mapView1 = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
add it to the view as addSubview(mapView1)
.
Hope this helps you!
Upvotes: 0