Mapview camera zoom not getting set for the Current location - swift

I want to set a proper zoom level for the User's current location using the Mapkit . I tried solutions from the stack but not working exactly for me . Can you guys please help me with the thing .
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
for point in points {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: point["latitude"] as! Double, longitude: point["longitude"] as! Double)
mapView.addAnnotation(annotation)
}
** let region = MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: CLLocationDistance(exactly:9000)!, longitudinalMeters: CLLocationDistance(exactly: 9000)!)
mapView.setRegion(mapView.regionThatFits(region), animated: true) **
I tried this code but its not getting zoomed properly at users location .
Any help is appreciated .

Related

MapKit, Set current location manually ? It is possible?

How to display current location from coordinates manually? I am taking coordinates from another GPS device. How to set it manually?
Where the coordinates come from is largely immaterial: as long as you have one you can set the map to display an area including that point. If you want to display the point itself you can add an annotation:
A simple example method to drop a pin and zoom in to its location:
func createAnnotation(from coordinate: CLLocationCoordinate2D, title: String) -> MKPointAnnotation {
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = title
return annotation
}
func dropPinAndZoomIn(to coordinate: CLLocationCoordinate2D){
var spanDelta = 0.035 //the width/height of the map area in degrees
let annotation = createAnnotation(from: coordinate, title: "My Location")
mapView.removeAnnotations(mapView.annotations) //clear any prev annotations
mapView.addAnnotation(annotation)
let span = MKCoordinateSpan(latitudeDelta: spanDelta, longitudeDelta: spanDelta)
let region = MKCoordinateRegion(center: coordinate, span: span)
let displayRegion = mapView.regionThatFits(region) //ensure the region can be displayed in the mapView's view
mapView.setRegion(displayRegion, animated: true)
}

Showing multiple 'GMSMarker' markers on Google maps

I am trying to show multiple location using Google maps, the code showing only one location. I am using for loop. I checked the code using MKMapView and it's working.
Here is the code:
let dict = [self.jsonElement]
for dicts in dict {
let latiCon = (dicts.value(forKey: "lati") as! NSString).doubleValue
let longiCon = (dicts.value(forKey: "longi") as! NSString).doubleValue
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: latiCon, longitude: longiCon, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latiCon, longitude: longiCon)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
I need to show multiple locations as I said, I have a look to relative answers but I did not find anything match with my question.
this code
let camera = GMSCameraPosition.camera(withLatitude: latiCon, longitude: longiCon, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
makeing a new mapView every time in loop so it's natural what you get.
a new mapview + one marker .
move this code outside loop or delete it if you already have a mapview declared before.

LocationManager.startUpdatingLocation finishes too late , can't get current location

I am trying to change the initial zoom location of my MapView.
I want the initialLocation to be (35,35) if myLocation == false and get the current Location if true. But startUpdatingLocation is finished after the initialLocation , so I cant get it and it crashes. If I use a breakpoint it works and gets the values.
if self.myLocation == false {
self.initialLocation = CLLocation(latitude: 35.00, longitude: 35.00) }
else {
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
checkLocationAuthorizationStatus()
locManager.startUpdatingLocation()
self.initialLocation = CLLocation(latitude: locManager.location.coordinate.longitude, longitude: locManager.location.coordinate.longitude)
}
centerMapOnLocation(self.initialLocation)
}
You need to use the locationManager delegate: locationManager:didUpdateLocations to set the location.
Place your self.initialLocation= code in the delegate.
Then create another function that you can call to execute your centerMapOnLocation(self.initialLocation) code.

MapKit default zoom

For some reason my market won't zoom to the local region. In fact, it does not change the zoom no matter what I change. Am I loading it in the wrong part of the code, or am I missing something else? Any help is appreciated.
My code is:
#IBOutlet var here: MKMapView!
var locationManager: CLLocationManager?
var selectedItem: String?
func locationManager
(manager: CLLocationManager!,didUpdateToLocation newLocation: CLLocation!,
fromLocation oldLocation: CLLocation!){
manager.desiredAccuracy = kCLLocationAccuracyBest
var region = self.here.region as MKCoordinateRegion
self.here.setRegion(region, animated: true)
region.span.longitudeDelta = 0.0144927536
region.span.latitudeDelta = 0.0144927536
}
I believe you need to set the lat and long BEFORE you set the region. The setRegion function is what zooms in on a certain part, and the level of the zoom depends on your span. Here's an example of a map that zooms.
let span = MKCoordinateSpanMake(0.075, 0.075)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: lat, longitude: long), span: span)
mapView.setRegion(region, animated: true)

Wrong Location - located in San Francisco and not in France

I am in France, and when I try my new func locationManager function, my map view locate me in San Francisco. Any idea?
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations[locations.count-1] as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}
I am using the simulator.
The iOS Simulator defaults to US - San Fran, and does not give you an estimate of your Mac's actual location.
However you can simulate movement or even a specific location like so:
Under the Debug menu in the Simulator, the last entry is "Location"; this gives you a sub menu with:
None
Custom Location
Apple Stores
Apple
City Bicycle Ride
City Run
Freeway Drive
Custom Location lets you enter a Lat/Long value.
Bicycle ride, City Run, and Freeway Drive are simulation of a moving location (in Cupertino, of course).
In addition to Woodstock's answer, you can also set a default location in the scheme. It's in Run/Options. There's an option to allow Location simulation and a bunch of standard defaults you can use for testing. For even more option, you can add a GPX file to your project.