Why GMSMapView is not set at the appropriate coordinate? - ios

This might be a frequently asked question, but believe it or not, I didn't find an answer that helped me fix my issue.
Nevertheless I tried multiple solutions and I don't understand why sometimes the camera of the GMSMapView object does not initialize at the right position.
I have dragged an UIView in my UIViewController scene from the Interface Builder, set it as GMSMapView object.
Here is my setCamera method :
private func setCamera(lat: Double, long: Double, zoom: Float) {
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: zoom)
mapView.camera = camera
mapView.setMinZoom(5, maxZoom: 15)
}
I tried to call it in viewDidLoad or viewWillAppear it's the same behavior.
1 out of 5 launches (approximately), the camera is not centered at the right coordinate, it feels like the GMSMapView is initialized with random camera.
How can I fix it ?

It's my initialization of GMSMapView:
let camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate,
zoom: mapDefaultZoom)
let mapView = GMSMapView.map(withFrame: view.bounds,
camera: camera)
view.addSubview(mapView)
It's update of the camera position:
let bounds = GMSCoordinateBounds().includingCoordinate(position)
let update = GMSCameraUpdate.fit(bounds,
withPadding: 60)
mapView.animate(with: update)
Hope, it will help you.

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)

Change the camera position of GMS Map View with animation

I have tried to zoom in and zoom out the GMSCamera but failed to perform this with animation. I can have gone through the documentation as well. but nothing helped.
UIView.animate(withDuration: 5.0) {
let zoom = GMSCameraUpdate.zoom(to: 15.0)
self.mapView.animate(toZoom: zoom)
}
The above code does not animate the mapview.
Try the below code. You may update it for latest swift syntax.
mapView.camera = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 1)
CATransaction.begin()
CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
let city = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 15)
self.mapView.animateToCameraPosition(city)
CATransaction.commit()
You do not need to use UIView.animate on google map as google map has its own method to animate with zoom level.
You just need to call this method.
let lat = 21.78841 //latitude of the location to display on map
let lng = 72.25478 //longitude of the location to display on map
let camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: lat, longitude: lng), zoom: 17)
mapView.animate(to: camera)
From Google's Doc.
/** Animates the camera of this map to |cameraPosition|. */
- (void)animateToCameraPosition:(GMSCameraPosition *)cameraPosition;

GMSMapview Handling Rotation and Car Rotation ios

I'm using google maps. Im getting user current location with course(angle) from cllocationmanger. After getting the location I'm adding to the GMSMapview.
Now my problem is car movement and map rotation. I want my map view to behave like googlemaps navigation. What I'm doing is after getting course of location I'm assigning it bearing of GMSMapview and not rorating the car.
But in navigation app they are rotating navigation arrow and also the map changing to north direction when ever there is a turn(left or right).
To achieve that do I need use both course and heading of CLLocationmanager.
here is my code. when ever there is a chnage in location im calling this method.
func ShowcurrentMarker()
{
currentlocationCoordinate = CLLocationCoordinate2DMake(LatestLocation.coordinate.latitude, LatestLocation.coordinate.longitude)
if(CurrentLocationMarker == nil)
{
CurrentLocationMarker = GMSMarker(position:currentlocationCoordinate)
CurrentLocationMarker.map = self.mapView
CurrentLocationMarker.icon = UIImage.init(named:"car_icon")
self.mapView.camera = GMSCameraPosition.camera(withLatitude: LatestLocation.coordinate.latitude, longitude: LatestLocation.coordinate.longitude, zoom: 18, bearing: CLLocationDirection.abs(LatestLocation.course), viewingAngle: 65)
}
else
{
CATransaction.begin()
CATransaction.setAnimationDuration(2)
CurrentLocationMarker.position = currentlocationCoordinate
self.mapView.camera = GMSCameraPosition.camera(withLatitude: LatestLocation.coordinate.latitude, longitude: LatestLocation.coordinate.longitude, zoom: 18, bearing: CLLocationDirection.abs(LatestLocation.course), viewingAngle: 65)
CATransaction.commit()
}
}
Please help me..
Thank you...

Moving Google Maps Camera to a Location

I'm working with a Google Maps View and I want to add a button to the map that when tapped, will move the camera to a specific location. I currently have a button outlet and an action connected to the button.
#IBAction func locationTapped(_ sender: Any) {
print("tapped")
let location = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)
mapView.camera = location
}
place exists but for some reason, the camera will not budge. I've tried different versions of code and looked at the Google Maps documentation but none of the options are producing results. Can anyone tell me what I'm doing wrong?
The GMSMapView class has the following function:
animate(to: GMSCameraPosition)
So in your code sample, instead of doing this:
mapView.camera = location
Try doing this:
mapView.animate(to: location)
Hope this helps!
in Swift3 and Swift4
for moving marker to current position use this:
func myLocationBtnAction(_ sender: UIButton) {
mapView.moveCamera(GMSCameraUpdate.setTarget(CLLocationCoordinate2D(latitude: (mapView.myLocation?.coordinate.latitude)!, longitude: (mapView.myLocation?.coordinate.longitude)!), zoom: 16))
and for a specific location use this:
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 16)
mapView?.camera = camera
mapView?.animate(to: camera)
and don't forget to extend GMSAutocompleteViewControllerDelegate for current location
Swift 2.3
This code is used for my purpose. In which marker tap event used, which moves camera position of map. Hope you find your solution.
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
mapView.selectedMarker = marker
var point = mapView.projection.pointForCoordinate(marker.position)
let camera = mapView.projection.coordinateForPoint(point)
let position = GMSCameraUpdate.setTarget(camera)
mapView.animateWithCameraUpdate(position)
return true
}
For Objective-c the method is:
[mapView moveCamera:[GMSCameraUpdate setTarget:<CLLocationCoordinate2DMake>]];
Maybe this is to late but i resolve that problem with adding this:
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500), execute: {
let camera = GMSCameraPosition.camera(withLatitude: lat!, longitude: lon!, zoom: 17.0)
self.mMap.animate(to: camera)
})
You have to wait until map load delegate
func moveMapCamera(at cordinate: CLLocationCoordinate2D, animated: Bool = false) {
let camera = MKMapCamera()
camera.centerCoordinate = cordinate
camera.pitch = 0
camera.altitude = 9000
mapV.setCamera(camera, animated: animated)
}

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

Resources