Layering a table view and a google map view iOS Swift - ios

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

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 adding view programmatically covers UI controls in interface builder

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
}

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 {
}

Adding a marker to a GMSMapView subview ios

I'm using the Google maps cocoapod and I can't figure out how to add a marker to my map. My map is a in subview. I put it in an array of views in viewDidLoad because for some reason I don't understand it always gets deleted before I can access it in methods outside of viewDidLoad - not ideal, but it was the only way I could figure out to save it. Anyway, I'm trying to add a marker. When I set the map to my main view like this it works fine:
var camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.7833, longitude: -122.4167, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
self.view = mapView
var marker = GMSMarker()
marker.position = camera.target
marker.map = mapView
However, when I try to add the marker to the subview I get from the array of subviews like this, it doesn't show up:
var tempMapSubView = self.views[0] as! GMSMapView
var camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.7833, longitude: -122.4167, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
tempMapSubView.camera = camera
tempMapSubView = mapView
var marker = GMSMarker()
marker.position = camera.target
marker.map = tempMapSubView
The array of subviews solution works really well with all the other stuff I'm trying to do. Thanks for your help!
Your code is getting the map from self.views[0], then creating a new map, then adding a marker to that new map. Then you do nothing with the new map, so it won't be displayed. Nothing has been added to the original map, and so you don't see the marker.
I think you need something like this:
var tempMapSubView = self.views[0] as! GMSMapView
var position = CLLocationCoordinate2DMake(37.7833, -122.4167)
var marker = GMSMarker()
marker.position = position
marker.map = tempMapSubView
Are you using google maps to get your device sensor via IP or GPS? If So it should automatically place a marker. You can then pass the marker coordinates to a string.

Resources