Swift adding view programmatically covers UI controls in interface builder - ios

I'm working on an ios app in which I want to show a google map view and a text label containing the location information in the same super view. My expected scenario is the map view fills the whole screen and the text label hover above it. When I do this, I create the map view in code but drag the label from object library in interface builder. However, it seems controls in IB will be initialized before ViewDidLoad is called. So view.addSubview(mapView) will add the map view but cover all the controls in IB. Is there any way to solve this problem if I still want to use IB building my UI? Thanks!
override func viewDidLoad() {
super.viewDidLoad()
// Set up Maps
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self
placesClient = GMSPlacesClient.shared()
let defaultLocation = CLLocation(latitude: -33.86, longitude: 151.20)
let camera = GMSCameraPosition.camera(withLatitude: defaultLocation.coordinate.latitude,
longitude: defaultLocation.coordinate.longitude,
zoom: zoomLevel)
mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
mapView.settings.myLocationButton = true
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.isMyLocationEnabled = true
// Add the map to the view, hide it until we've got a location update.
view.addSubview(mapView)
mapView.isHidden = true
}

Related

Is there a way to have InfoWindow open on App load in Google Maps API?

Currently my app opens a custom infowindow when the MapView delegate calls the didTap protocol. This then calls a function which initializes the needed aspects of the infowindow shown below ...
var tappedOverlay : GMSOverlay?
var customInfoWindow : CustomInfoWindow?
var mapView: GMSMapView!
override func viewDidLoad(){
super.viewDidLoad()
self.customInfoWindow = CustomInfoWindow().loadView()
let camera = GMSCameraPosition.camera(withLatitude: 37, longitude: -77, zoom: 16.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
//this is where i attempt to force a infoWindow to open
tappedOverlay = overlay
initInfoWindow(overlay: overlay)
}
fun initInfoWindow(overlay: GMSOverlay){
mapView.animate(toLocation: position!)
let point = mapView.projection.point(for: position!)
let newPoint = mapView.projection.coordinate(for: point)
let camera = GMSCameraUpdate.setTarget(newPoint)
mapView.animate(with: camera)
let background = UIColor.infoWindowBackground
customInfoWindow?.layer.backgroundColor = background.cgColor
customInfoWindow?.layer.cornerRadius = 8
customInfoWindow?.center = mapView.projection.point(for: position!)
customInfoWindow?.center.y -= 100
customInfoWindow?.CustomWindowLabel.text = overlay.title
customInfoWindow?.CustomWindowSubLabel.lineBreakMode = .byWordWrapping
customInfoWindow?.CustomWindowSubLabel.numberOfLines = 0
mapView.addSubview(customInfoWindow!)
}
func mapView(_ mapView: GMSMapView, didTap Overlay: GMSOverlay){
initInfoWindow(overlay: overlay)
}
Now I would like to have the app automatically open a certain infowindow as soon as it loads.
When I try to manually display the infoWindow on load by calling the function in viewDidLoad(), I have to comment out the mapView.animate(with: camera) line otherwise the map tiles will never load. Someone pointed out to me that the mapView methods have some 'background' information about the overlay when they are called, so I added the tappedOverlay = overlay line, this gets close in that part of the infoWindow is displayed when the app is loaded. However, this and all other 'regular' infoWindows appear without a background and not centered on the point and cannot be interacted with.
The infoWindow's are a subclass of UIView loaded from a XIB file.
The above code is what I thought would be relevant extracted from a larger code base. Let me know if anything needs to be added!
Any help is appreciated!
Maybe this answer will help someone else. The issue was in the mapView.projection functions, they were returning essentially default values. I'm not entirely sure, but it seems when you click on a marker the mapView changes part of the frame = ( 0 0;) variable in the background, so this lead me to change the mapView initialization.
So instead of ...
let camera = GMSCameraPosition.camera(withLatitude: 37, longitude: -77, zoom: 16.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
We need ...
let camera = GMSCameraPosition.camera(withLatitude: LAT_OF_YOUR_INFOWINDOW,
longitude: LONG_OF_YOUR_INFOWINDOW,
zoom: 16.0)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)

Google Maps API for iOS - Marker is not displayed

I'm new to coding for iOS and I wanted to have a Map with a path/way on it in my Swift App. After trying to apply an KML Overlay, which didn't work, I decided to use a polyline. But the polyline wasn't desplayed either, so I tried a Marker, exactly following the tutorial at https://developers.google.com/maps/documentation/ios-sdk/marker, but the marker is also not displayed. What am I doing wrong? Here is my code for the Marker:
import UIKit
import GoogleMaps
class MapViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GMSServices.provideAPIKey("MY_KEY")
let camera = GMSCameraPosition.camera(withLatitude: 48.194857, longitude: 13.955710, zoom: 17)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
mapView.mapType = .satellite
mapView.setMinZoom(17, maxZoom: 19)
view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 13.9553836,longitude: 48.1956954)
marker.title = "Hello World"
marker.map = mapView
// Do any additional setup after loading the view.
}
}
Thank you in advance!
I think the code is okay, but the marker is not visible because it is in another part of the world.
Try to exchange latitude and longitude of your marker
marker.position = CLLocationCoordinate2D(latitude: 13.9553836,longitude: 48.1956954)
Or set a lower zoom to your map

Swift 4 GoogleMaps add UIToolBar / make map "smaller" to fit other UI elements

Perhaps this is the exact same question as this other post
However, I need a more step by step / tutorial type answer.
I want to add a UIToolBar item at the top of my GoogleMap. But the map takes up the entire view. I've searched everywhere and every tutorial, but I can't figure this very simple thing out. If part of the solution is adding a new swift file and placing the 'GMSMap' code there and then changing the original view controller class - that is fine, but I need just a tiny bit more step by step
I've tried play with "withFrame", however that does not seem to help
import UIKit
import GoogleMaps
class FirstViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var mapView:GMSMapView!
var latitude:Double = 0
var longitude:Double = 0
var zoom:Float = 15
let locationManager = CLLocationManager()
var userLocation:CLLocationCoordinate2D!
override func viewDidLoad() {
super.viewDidLoad()
// Setup simple mapview and camera
let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: zoom)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.mapView.delegate = self
UPDATE
Things are somewhat better, but there is still a nagging problem.
I have a new repo here
I added two UIViews inside the default View:UIView and added constraints. The 'Map View is type GMSMapView. And I added a view at the top of the scene 'Button View' with a button. Now my UI looks like I want it.
I also created an IBOutlet for GMSMapView and wired it to 'Map View'.
HOWEVER
The map that is displayed is ignoring the latitude and longitude variables. No matter what I set them to, I always see France and other parts of Western Europe. In the example below, I'm using coordinates for San Francisco
class ViewController: UIViewController, GMSMapViewDelegate {
#IBOutlet weak var mapView:GMSMapView!
var latitude:Double = 37.7749
var longitude:Double = 122.4194
var zoom:Float = 12
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: zoom)
let myGSMMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView = myGSMMap
self.mapView.delegate = self
}
}
Images for clarity: https://imgur.com/a/vvmD5 . (the green in the upper portion of the screen is the UIView, and below it you will see the map)
Just write below code in your viewWillAppear()
Self.mapView.camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: zoom)
It will move your map camera according to latitude and longitude.
And remove what your write-in viewDidLoad()
Check this http://www.ryanwright.me/cookbook/ios/obj-c/maps/gmap/subview. It might be helpful. You have a detailed explanation given.

Adding marker on Google maps

This may be a repeated question (looked at many answers before writing it), but I am unable to solve it.
I want to simple integrate google maps into my swift app. All is working great except getting that marker to show on the map. I am following google map api docs, but no luck.
Here is the body of the function:
// Do any additional setup after loading the view.
/**Testing Google Maps **/
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
//let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
googleMapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = googleMapView
This is executing in viewDidLoad(). I get the map to show but its not showing any marker.
Thanks for the help.
This code works for me:
var marker = GMSMarker()
marker.postion = CLLocationCoordinate2DMake(10,10)
marker.title ="ENTER TITLE"
marker.snippet ="ENTER SNIPPET"
marker.map = googleMapView
Hope this helps :)
After long trying and researching, got it to work by tweaking one line to get the same map view in the UIView with type GMSMap:
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
googleMapView.camera = camera //This line
googleMapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
did you set the delegate of mapView in viewDidLoad
mapView.delegate = self
and accept GMSMapViewDelegate protocol in your ViewController like this
class ViewController: UIViewController, GMSMapViewDelegate {
}

Layering a table view and a google map view iOS Swift

I'm trying to set up a map search using the google maps sdk in ios. So far, I have the autocompleted search working correctly and when I search, the results show up in a tableview. I want to be able to click on a cell in the table view and reveal the map, then show the table again when the user touches back into the search box. My strategy to do this is to layer the mapview and the table view in the same view Controller, and show/hide or push whichever view to the top. However, because the google map is not part of mapkit, I'm not sure that I can manipulate it like this. Please let me know if you know an easy way to do this. Setting the view to tableview also results in a black screen
mapView.hidden just reveals a black screen
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
view.bringSubviewToFront(self.tableView)
placesClient = GMSPlacesClient()
var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
//mapView.hidden = true
mapView.myLocationEnabled = true
self.view = mapView
self.view = self.tableView
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
I am using the google maps cocoapod
This depends on the order which they have in the view controller's list of subviews. If mapView and tableView are both direct subviews of the root view, you can just call insertSubview:belowSubview:
[self.view insertSubview:mapView belowSubview:tableView];
and vice versa.
I found out that I had to add my own subview, change the class of that subview to GMSMapview and assign the map to that subview.
var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.mapSubView.camera = camera
self.mapSubView = mapView

Resources