'Google Places acting up

This is my first time using Google Maps and I thought I got it working but when I sent it to Apple they told me it wasn't brining up supermarkets but showing random businesses. I have introduced this to my app which is a kind of shopping list and in a different tab should show google places with all the supermarkets nearby. No search bar etc required. There are two problems:

  • The map seems to dominate the entire view and takes over the tab bar. I can't seem to resize it.
  • The map refreshes every couple of seconds when I test it on a device but seems to actually bring up supermarkets.

Help!

Here I have the view controller file:

import Foundation
import GoogleMaps
import MapKit
import CoreLocation
import UIKit


class ShopFinderViewController: UIViewController, CLLocationManagerDelegate{
    
    let manager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        
        GMSServices.provideAPIKey("#####################")
        
        print("License: \n\n\(GMSServices.openSourceLicenseInfo())")
    }
    let googlePlacesCoordinator = GooglePlacesCoordinator()
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else{
            return
        }
        let coordinate = location.coordinate
        let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 15.0)
        let mapView = GMSMapView.map(withFrame: view.frame, camera: camera)
        view.addSubview(mapView)

        
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

        googlePlacesCoordinator.fetchLocation(for: coordinate) { places in
            
            places.results.forEach { place in
                let loc = CLLocationCoordinate2D(
                    latitude: place.geometry.location.lat,
                    longitude: place.geometry.location.lng)
                let placeMaker = GMSMarker(position: loc)
                placeMaker.title = place.name
                placeMaker.map = mapView
            }
            
        }
        marker.map = mapView
    }
    
}

And here's the coordinator file:

import Foundation
import CoreLocation


struct GooglePlacesCoordinator {
    let formatURL: String =
    "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&radius=1000&type=supermarket&key=#####################"

    func fetchLocation(for coordinate: CLLocationCoordinate2D, completion: @escaping (GMSPlaceField) -> Void) {
        let urlString = String(format: formatURL, coordinate.latitude, coordinate.longitude)
        let url = URL(string: urlString)!
        let urlRequest = URLRequest(url: url)
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: urlRequest) { data, res, error in
            if let error = error{
                print(error)
                return
            }
            if let res = res as? HTTPURLResponse,
               res.statusCode >= 400 {
                print(res.statusCode)
                return
            }
            if let data = data {
                if let results = try? JSONDecoder().decode(GMSPlaceField.self, from: data) {
                    DispatchQueue.main.async {
                        completion(results)
                    }
                }
            }
        }

        task.resume()
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source