Fatal error upon building maps app ios - ios

Getting an error, I've been searching all day on how to use Corelocation along with GoogleMaps. thought I already had it when i got an error at the GMScameraposition part of the code. basically i'm trying to get my location upon loading the view which is why most is within the viewdidload().
import UIKit
import GoogleMaps
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
var currentLocation: CLLocation!
var long: Float64!
var lat: Float64!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.cameraWithLatitude(lat,
longitude: long, zoom:6)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Hello World"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
self.view = mapView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.last! as CLLocation
long = currentLocation.coordinate.longitude;
lat = currentLocation.coordinate.latitude;
}
}

Your view loads before you have an updated location to fill your variables, so it's as you said in your comment, your long and lat variables have no value.
I wrote out the fixed viewDidLoad and didUpdateLocations functions for you here:
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.cameraWithLatitude(0,
longitude: 0, zoom:0) //this can be set to wherever you want your view to start at
//UPDATE: changed the initializing frame to be equal to the view, instead of CGRectZero
let mapView = GMSMapView.mapWithFrame(self.view.frame, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Hello World"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
self.view = mapView
}
//updated! This will tell your camera to snap to your latest location once you've received it.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.last
mapView.camera = GMSCameraPosition(target: currentLocation.coordinate, zoom: 6, bearing: 0, viewingAngle: 0)
}
}
Let me know if this works out for you.

Related

Getting location in swift 5

I realize there are lots of other pages that attempt to solve the challenge of getting user location using CLLocationManager() in ios swift but they don't work for me which is why I created this question. My code is below which I believe correctly implements the CLLocationManagerDelegate but the call to locationManager.startUpdatingLocation() never calls the locationManager function. This makes the Google Map simply open to a random position. I've simulated certain locations in the scheme and they work great like South Africa
example of Google Map with simulated location:
However, when I uncheck the simulation, the location is never found. Any advice or help would be appreciated. Not sure if this is a problem with my computer or code.
import UIKit
import GoogleMaps
class Map: UIViewController, CLLocationManagerDelegate {
var mapView: GMSMapView?
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 40.7, longitude: -74.0, zoom: 4.0))
view = mapView
if (CLLocationManager.locationServicesEnabled()){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let center = CLLocationCoordinate2D(latitude: locations.last!.coordinate.latitude, longitude: locations.last!.coordinate.longitude)
let camera = GMSCameraPosition.camera(withLatitude: center.latitude, longitude: center.longitude, zoom: 4.0);
self.mapView?.camera = camera
self.mapView?.isMyLocationEnabled = true
let marker = GMSMarker(position: center)
print("Latitude :\(center.latitude)")
print("Longitude :\(center.longitude)")
marker.map = self.mapView
marker.title = "Current Location"
locationManager.stopUpdatingLocation()
}
}

viewDidLoad resetting Google Maps view after updating the current location

I'm still fairly new to swift/xcode so this may just be a simple issue with viewDidLoad that I am over looking but here's whats happening. I am working on an application that gets your current location and displays the closest ski mountains around you using Google Maps/Places API. If I run the scene directly from the Navigation controller, the map displays properly with all the markers for the mountains. However I am implementing a log in function and therefore if I load the scene after logging in, the maps just resets to the original coordinates in the viewDidLoad function. My code is posted below. My question is first, how do i fix it so that it doesn't reset to the original coordinates and secondly, why does it do this only when I run it from a scene that isn't the navigation controller?
import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation
class MountainFinderController: UIViewController, CLLocationManagerDelegate {
let mountainModel = skiMountainData()
var placesClient: GMSPlacesClient?
var locationManager = CLLocationManager()
lazy var mapView = GMSMapView()
var currentLocation: CLLocationCoordinate2D!
var mountainMarkers = [GMSMarker]()
typealias JSONDictionary = [String: Any]
override func viewDidLoad() {
super.viewDidLoad()
//creates initial map view
let cameraGMAPS = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 8)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: cameraGMAPS)
mapView.isMyLocationEnabled = true
self.view = mapView
print("created init map")
//requests authorization to use location
locationManager.requestAlwaysAuthorization()
self.locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
print("done loading view")
}
override func loadView() {
}
//called when startUpdatingLocation is called
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//gets user location
let userLocation = locations.last
currentLocation = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
//moves the camera to the users current location and shows their current location on the map
let cameraGMAPS = GMSCameraPosition.camera(withLatitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude, zoom: 8)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: cameraGMAPS)
mapView.isMyLocationEnabled = true
self.view = mapView
//function call to get nearest 20 mountains
mountainModel.getNearbyMountains(lat: locationManager.location!.coordinate.latitude, long: locationManager.location!.coordinate.longitude)
//adds a marker for each mountain
print("adding markers")
addMarkers()
print("added markers")
//stops updating the location
locationManager.stopUpdatingLocation()
}
//func to add a marker for each mountain to the map
func addMarkers(){
mountainMarkers.removeAll()
for currMountain in mountainModel.getSkiMountainData() {
let mountainMarker = GMSMarker()
mountainMarker.position = CLLocationCoordinate2D(latitude: currMountain.getLat(), longitude: currMountain.getLong())
mountainMarker.title = currMountain.getName()
mountainMarker.map = mapView
mountainMarkers.append(mountainMarker)
}
mountainModel.setMountainMarkers(markers: mountainMarkers)
}
}
Figured it out. Drewster was right i had to update the
mapView with mapView.camera = cameraGMAPS
Thank you guys

myLocationEnabled changing to isMyLocationEnabled on swift when using GoogleMaps

Im trying to get a users current location to show with google maps API. I have been struggling with this issue for weeks and the code runs but doesn't update to current location when I change the location in the simulator.
My code is below and after looking how other people are doing it they all have mapView.myLocationEnabled = true whereas for me this produces an error saying 'myLocationEnabled has been renamed to 'isMylocationEnabled'. I've seen some up to date posts (from 2 months ago) and everyone is using myLocationEnabled without an error it seems, why am I getting this? could this be the reason why my current location is not updating?
import UIKit
import GoogleMaps
class FirstViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
mapView.settings.myLocationButton = true
mapView.settings.compassButton = true
}
override func viewDidAppear(_ animated: Bool) {
locationManager.requestWhenInUseAuthorization()
}
func locationManager(manager: CLLocationManager , didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager ,didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
let marker = GMSMarker()
marker.title = "Current Location"
marker.map = mapView
locationManager.stopUpdatingLocation()
}
}
}
SO I FINALLY SOLVED THIS! Basically I updated my existing Podfile with GoogleMaps and GooglePlaces and then updated pod to ensure all the frameworks are working. Then I used this code to get my current location to work
import UIKit
import GoogleMaps
class FirstViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()
override func viewDidLoad() {
super.viewDidLoad()
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
vwGMap.camera = camera
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 500
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
self.view = vwGMap
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedWhenInUse)
{
vwGMap.isMyLocationEnabled = true
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last
vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
vwGMap.settings.myLocationButton = true
self.view = self.vwGMap
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
marker.map = self.vwGMap
}
}

current location update on Google Maps SDK for Xcode not working

I have been trying to implement google maps in my application. The first step what I want to do is to simply get is:
1. Get the app to ask permission to use current location
2. Find the users current location and have a blue dot marker to mark it.
3. Update and centre the map around the users current location.
I have tried various methods to get this working but always end up with an error or doesn't work. Initially when I simulated the code below, it did come up with the dialog about requesting current location permission and a blue dot but in the following simulations this seems to have disappeared and when changing my current location in simulator (by going to edit schemes and changing default location) , the new current location did not update. I have a feeling that the code isn't working beyond viewDidLoad. where am I going wrong?
Please can I get some advice on how to rectify these problems and get the above points working (as well as the simulator to always ask for permission for current location). Currently the code runs with no errors but only shows me a map of the UK. (I have already added NSLocationWhenInUse and NSLocationAlwaysUsage.... into info.plist. And I have disabled all breakpoints. I have also tried resetting simulator)
import UIKit
import GoogleMaps
class FirstViewController: UIViewController,CLLocationManagerDelegate {
#IBOutlet weak var googlemaps: GMSMapView!
//this is the connection from the storyboard to the view controller
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 51.508742, longitude: -0.087891, zoom: 8.0)
let viewMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = viewMap
viewMap.isMyLocationEnabled = true
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func loadView() {
super.viewDidLoad()
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
// verification of granted permission while app in use
let locationManager = CLLocationManager()
let mapView = GMSMapView()
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
// if permission granted- starting updating location
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
//this is suppose to be button that is added when tapped centers to users location
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition.camera(withTarget: location.coordinate, zoom: 20)
self.view = mapView
locationManager.stopUpdatingLocation()
}
}
}
}
}
Thanks in advance! I have spent days trying to work this out with no success :(
I was able to solve this problem by updating Cocoapods and ensuring that GoogleMaps is in my Podfile. Then in my viewController I had the following code which executed perfectly.
import UIKit
import GoogleMaps
class FirstViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()
override func viewDidLoad() {
super.viewDidLoad()
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
vwGMap.camera = camera
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 500
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
self.view = vwGMap
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedWhenInUse)
{
vwGMap.isMyLocationEnabled = true
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last
vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
vwGMap.settings.myLocationButton = true
self.view = self.vwGMap
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
marker.map = self.vwGMap
}
}

Current Location in Google Maps with swift

I'm trying to display the user's current location on a google map but in the case below, the map doesn't even get displayed. What should I change to fix this?
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//user location stuff
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error" + error.description)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 8)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
let marker = GMSMarker()
marker.position = center
marker.title = "Current Location"
marker.snippet = "XXX"
marker.map = mapView
locationManager.stopUpdatingLocation()
}
You can try this bellow code its working fine
import UIKit
import GoogleMaps
import GooglePlaces
class SearchMapsViewController: UIViewController,
UINavigationBarDelegate, GMSAutocompleteFetcherDelegate,
LocateOnTheMap, UISearchBarDelegate, CLLocationManagerDelegate
{
#IBOutlet var googleMapsContainerView: UIView!
var searchResultController: SearchResultsController!
var resultsArray = [String]()
var googleMapsView:GMSMapView!
var gmsFetcher: GMSAutocompleteFetcher!
var locationManager = CLLocationManager()
override func viewDidAppear(animated: Bool) {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
self.googleMapsView = GMSMapView (frame: self.googleMapsContainerView.frame)
self.googleMapsView.settings.compassButton = true
self.googleMapsView.myLocationEnabled = true
self.googleMapsView.settings.myLocationButton = true
self.view.addSubview(self.googleMapsView)
searchResultController = SearchResultsController()
searchResultController.delegate = self
gmsFetcher = GMSAutocompleteFetcher()
gmsFetcher.delegate = self
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error" + error.description)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let userLocation = locations.last
let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude, zoom: 15);
self.googleMapsView.camera = camera
self.googleMapsView.myLocationEnabled = true
let marker = GMSMarker(position: center)
print("Latitude :- \(userLocation!.coordinate.latitude)")
print("Longitude :-\(userLocation!.coordinate.longitude)")
marker.map = self.googleMapsView
marker.title = "Current Location"
locationManager.stopUpdatingLocation()
}
do requier setting on infoPlist and then try this
#IBOutlet weak var your "name of view which show map": GMSMapView!
override func viewDidLoad(){
super.viewDidLoad()
placesClient = GMSPlacesClient.shared()
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 500
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
mapView.settings.myLocationButton = true
mapView.settings.zoomGestures = true
mapView.animate(toViewingAngle: 45)
mapView.delegate = self }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last // find your device location
mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 14.0) // show your device location on map
mapView.settings.myLocationButton = true // show current location button
var lat = (newLocation?.coordinate.latitude)! // get current location latitude
var long = (newLocation?.coordinate.longitude)! //get current location longitude
}
The problem is that you are setting the mapView's frame to CGRectZero. This causes the map to have zero height and zero width, no wonder it does not show!
Try setting it to CGRectMake(0,0,200,200) for example, this will give you a map at the left top of the screen with a size of 200 x 200.
I have never used Swift before, so the syntax might be a little different for CGRectMake()
It seems that your creation of the map isn't in your viewDidLoad function. You may want to try moving that there and see what happens.
Add the appropriate properties into the info.plist.
You should put make sure you have the NS properties of locationalwaysusagedescription and wheninuseusagedescription in the information properties list. This allows for the permissions of the current location to be asked.
import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation
class MapsViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var mapView = GMSMapView()
var locationManager = CLLocationManager()
let marker = GMSMarker()
override func viewDidLoad(){
super.viewDidLoad()
mapView.frame = self.view.bounds
self.view.addSubview(mapView)
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 10
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
mapView.settings.myLocationButton = true
mapView.settings.zoomGestures = true
mapView.animate(toViewingAngle: 45)
mapView.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last // find your device location
mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 14.0) // show your device location on map
mapView.settings.myLocationButton = true // show current location button
let lat = (newLocation?.coordinate.latitude)! // get current location latitude
let long = (newLocation?.coordinate.longitude)! //get current location longitude
marker.position = CLLocationCoordinate2DMake(lat,long)
marker.map = mapView
print("Current Lat Long - " ,lat, long )
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
mapView.clear()
DispatchQueue.main.async {
let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
self.marker.position = position
self.marker.map = mapView
self.marker.icon = UIImage(named: "default_marker")
print("New Marker Lat Long - ",coordinate.latitude, coordinate.longitude)
}
}
}

Resources