(iOS Swift)Google Maps animateWithCameraUpdate not working - ios

I am working with Google maps in iOS. I wanted to animate my Camera such that it shows all the required Lat, Lngs. I am storing those locations in the array(as CLLocationCoordinate2D instances) and using GMSCoordinateBounds class to add these locations. This is my code:
let bounds = GMSCoordinateBounds()
bounds.includingCoordinate(firstLocation)
bounds.includingCoordinate(lastLocation)
let camUpdate = GMSCameraUpdate.fit(bounds, withPadding: 60)
mMapView.camera = GMSCameraPosition.camera(withLatitude: firstLocation.latitude, longitude: firstLocation.longitude, zoom: 18)
mMapView.animate(with: camUpdate)
firstLocation and lastLocation have correct values but the camera is not animating. Am I missing something?

Very silly mistake. includingCoordinates function returns GMSCoordinateBounds object. I had to set to itself again for it to work like so:
bounds = bounds.includingCoordinate(firstLocation)
bounds = bounds.includingCoordinate(lastLocation)
If i set like above, it is working fine.

Related

Get google map currently rotated angle by user in iOS

I am using google map in the application. When I do not rotate map everything works fine. But when I rotate map I am getting problem as shown in image. To solve that I need to get the google map currently rotated angle by the user. I need to get this so that I can place the marker back on the map at the same angle. Currently my map angle after rotation and my overlay seems to be different after placing plan image on map even when I am getting correct top left & bottom right corner coordinates.
My code
let topCoordinate = CLLocationCoordinate2D(latitude: topLattitude, longitude: topLongitude)//top
let bottomCoordinate = CLLocationCoordinate2D(latitude: bottomLattitude, longitude: bottomLongitude)//bottom
overlayBounds = GMSCoordinateBounds(coordinate: topCoordinate, coordinate:
bottomCoordinate)
let overlay = GMSGroundOverlay(bounds: overlayBounds, icon: planImage)
overlay.map = mapView
GMSMapViewDelegate
Bearing of the camera, in degrees clockwise from true north.
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
print(position.bearing)
}
I have solved my problem by changing the overlay method as
let zoomLevel = (object["image_zoom_level"] as? NSString)?.doubleValue
let overlay = GMSGroundOverlay(position: centreCoordinate, icon: planImage, zoomLevel: CGFloat(zoomLevel!))
overlay.map = mapView
if let angle = (object["rotation_angle"] as? NSString)?.doubleValue{
overlay.bearing = angle
}
Instead of using Overlay bounds I have just used center coordinate and placed overlay image using image zoom level and rotation angle. By this, my image is placing in proper angle and at a proper place. This works fine if the Image size is correct as you want place and the center point is accurate and precise.

Limit map visibility to its overlay

I want to place an image over my map and limit the map visibility to the image overlay, so that the user can only see my image and not the map behind it. I tried to used the solution offered by Google Maps API with the GMSGroundOverlay, like so:
let southWest = CLLocationCoordinate2D(latitude: 40.712216, longitude: 32.22655)
let northEast = CLLocationCoordinate2D(latitude: 40.773941, longitude: 23.12544)
let overlayBounds = GMSCoordinateBounds(coordinate: southWest, coordinate: northEast)
let icon = UIImage(named: “someImg”)
let overlay = GMSGroundOverlay(bounds: overlayBounds, icon: icon)
overlay.bearing = 0
overlay.map = mapView
Also, I set the cameraTarget of the map to the bounding box:
mapView.cameraTargetBounds = overlayBounds
This does limit the user from scrolling the map infinitely, but I expected these limits to go as far as the overlay goes. Now the image looks like a sticker on the map, as I can still see the map around the image.
I am not sure I fully understand the way GMSCoordinateBounds and GMSGroundOverlay work..so I'm asking here if anyone can give me some hints on the adjustments that I can make to get what I want.

Google Maps iOS SDK Prevent camera zoom after each location update

I have an app which requires the user's location to be constantly updated so I can display their current coordinates and altitude. I'm doing this using the didUpdateLocations function:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
mapView.camera = GMSCameraPosition(target: locationManager.location!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
let locValue : CLLocationCoordinate2D = manager.location!.coordinate
let altitude : CLLocationDistance = Double(round(1000*manager.location!.altitude)/1000)
let long = Double(round(10000*locValue.longitude)/10000)
let lat = Double(round(10000*locValue.latitude)/10000)
let alt = String(altitude) + " m"
latitudeLabel.text = String(lat)
longitudeLabel.text = String(long)
altitudeLabel.text = alt
showLearningObjectsWithinRange(location)
}
}
The problem is, when I try to zoom in on a certain spot on the map, if I move the device even slightly the camera zooms back out again. Obviously this is because of the first line in my didUpdateLocations function setting the camera position, but if I remove that line, the map doesn't center to their location at all.
I tried moving the GMSCameraPosition code to the viewDidLoad, viewWillAppear, and several other places, but this caused the app to crash because it couldn't locate the user in time.
Does anyone have any suggestions on how to make this work? Thanks.
use this instead of that certain line (mapview.camera = ...)
mapView.animate(toLocation: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude))
With regards to implementing location update, there was an issue posted in GitHub - Implement Responsive User Location Tracking Mode due to location update optimization and going through the thread, a given workaround to show the user location on the map is to call
nMapView.setMyLocationEnabled(true);
instead of:
nMapView.setMyLocationTrackingMode(MyLocationTrackingMode.TRACKING_FOLLOW);
Then, with regards to camera zoom, as discussed in Camera and View - Zoom, you can try setting a minimum or maximum zoom to restrict zoom level. As stated,
You can restrict the range of zoom available to the map by setting a min and max zoom level.
You may also try the solutions given in this SO post - Google Maps iOS SDK, Getting Current Location of user. Hope it helps.

iOS MKMapView doesn't accept latitude value bigger than 45.x?

How to reproduce the error:
let mapView = MKMapView.init(frame: UIScreen.mainScreen().bounds)
mapView.region.center = CLLocationCoordinate2D.init(latitude: 60, longitude: 100)
mapView.region.span = MKCoordinateSpanMake(20, 20)
print(mapView.region.center)
self.view = mapView
And the print statement prints this:
CLLocationCoordinate2D(latitude: 44.880507991448255, longitude: 100.00000000000004)
The problem is that I actually set the latitude to 60 at line 2. However the resulted latitude is 44.88x. And I have tried other values above 45, and they are not correct also. Any ideas? Thanks!
This seems to be an issue with Swift. If you try this code in Objective-C
mapView.region.center = CLLocationCoordinate2D.init(latitude: 60, longitude: 100)
The compiler gives an error expression is not assignable. The correct approach is to create a new region and then assign this region to the map view:
let region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(60.0,100.0), MKCoordinateSpanMake(20, 20))
mapView.region = region

GMS Map View always showing Europe

I'm trying to use Google Maps SDK to display a map of my current location (Berkeley, CA) as the focus, but regardless of what lat/lon I put in, it always shows Europe, as shown below:
Here is the code for the map view:
let camera = GMSCameraPosition.cameraWithLatitude(37.8750360, longitude: -122.2573240, zoom: 1)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.delegate = self
I'm doing very similar things in other parts of my application, but have never gotten an issue. If someone knows how to fix this, please let me know! Thanks
Please make sure that you are test on real device. not be Simulator,
If you use Simulator than enter latitude and longitude manually.
Like this-
You can test is on simulator as well and you can change location without enter lat-long
for that you need to change location from your Xcode (above console Please rifler the below screen shot
)
Maybe this is because you are setting the mapView's frame to CGRectZero. This may cause the map to have zero height and zero width.
Example based from this tutorial:
override func viewDidLoad()
{
let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.8750360, longitude: -122.2573240, zoom: 1)
mapView = GMSMapView.mapWithFrame(self.view.frame, camera: camera)
mapView.camera = camera
}
Check these related questions:
Current Location in Google Maps with swift
Show GoogleMaps on View in Swift?
You're setting up your view before even setting your delegate (so your code isn't even attached to your instance of mapView). Fix it like this:
1: Make sure you've defined your GMSMapViewDelegate for the class
2: Set your delegates first
mapView.delegate = self
mapView.myLocationEnabled = true
let camera = GMSCameraPosition.cameraWithLatitude(37.8750360, longitude: -122.2573240, zoom: 1)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)

Resources