0

I am trying to create a mock up app that shows the campus of my school. I want to be able to search through the campus and then click the location button to have the screen follow my location. Check video below to understand my issue. My code is proprietary as i am brand new to swift and xcode. I am not using the story board, just programatically creating this app. When i do get the location to consistently update my location and follow my simulation location, it tears down the view and rebuilds it, which leads to it looking very glitchy. Also, i cannot break the update by changing moving the camera somewhere else. Im sure im missing something quite big. To make as much sense as possible, i want to be able to follow the location on screen in a smooth look, as well as stop the updating location so that can search the campus without my screen updating to my current location. video link: [https://www.dropbox.com/s/op2kw9sfk0c8dm5/Xcode%20problem.mp4?dl=0][1]

here is my code:

    import UIKit
    import GoogleMaps
    import CoreLocation


    class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate{

    lazy var mapView = GMSMapView()
    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        GMSServices.provideAPIKey("AIzaSyBXEZf5gq-ewIjmlzyWlsDHJSsOTsHCm4k")

        let camera = GMSCameraPosition.camera(withLatitude: 35.046462, longitude: -85.298153, zoom: 15.8, bearing: 295, viewingAngle: 0)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.mapType = .satellite
        //view = mapView

        //User Location
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        locationManager.stopUpdatingLocation()
        view = mapView
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 15.8, bearing: 295, viewingAngle: 0)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.mapType = .satellite
        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
        showMarker(position: CLLocationCoordinate2D.init(latitude: 35.046462, longitude: -85.298153), markerTitle: "Cardiac Hill", markerSnippet: "UTC")
        showMarker(position: CLLocationCoordinate2D.init(latitude: 35.047488, longitude: -85.300121), markerTitle: "Library", markerSnippet: "UTC")
        view = mapView         
    }

    func showMarker(position: CLLocationCoordinate2D, markerTitle: String, markerSnippet: String){
        let marker = GMSMarker()
        marker.position = position
        marker.title = "\(markerTitle)"
        marker.snippet = "\(markerSnippet)"
        marker.tracksViewChanges = true
        marker.opacity = 0.75
        marker.icon = GMSMarker.markerImage(with: .white)
        marker.map = mapView
    }

    func markerInfo(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        var view = UIView(frame: CGRect.init(x: 0, y: 0, width: 200, height: 70))
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 6

        let lbl1 = UILabel(frame: CGRect.init(x: 8, y: 8, width: view.frame.size.width - 16, height: 15))
        lbl1.text = "Cardiac label"
        view.addSubview(lbl1)

        let lbl2 = UILabel(frame: CGRect.init(x: lbl1.frame.origin.x, y: lbl1.frame.origin.y + lbl1.frame.origin.y + 3, width: view.frame.size.width - 16, height: 15))
        lbl1.text = "lbl2 info"
        view.addSubview(lbl2)

        return view
    }
}

Thank you for your time, I will be sure to check this almost everyday to respond to any addition information needed. PS, i already did api and sdk importing, as well as added info into info.plist. I seem to get everything to work but this issue.

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
  • Stop Initializing your camera obj in didUpdateLocation delegate.In Native MapView of iOS you can center the view of the map using setRegion method , search for the google doc and look for that similar method to realign the view of map to the center of user location.For setting map to center look into this https://stackoverflow.com/a/25451592 – Dipak Singh Feb 27 '18 at 06:04
  • @DipakSingh thank you very much for responding. I will try this tonight when I get home! – Stephen Weiler Feb 27 '18 at 21:22
  • Thank you so much for you help! Because your comment made 100% sense to me, i decided to try a different approach and use mapKit instead of google maps and got it working with 50% less code. I can post the resulting code if you want but now i have a flawless follow animation and when i pan away and click recenter it recenters on my location perfectly. – Stephen Weiler Feb 28 '18 at 03:54
  • Glad it helped you in your cause.No need posting the resulting code.I know the workaround. – Dipak Singh Mar 01 '18 at 05:57

0 Answers0