Determine proper zoom level on GMSMapView to fit all the locations needed? - ios

In my iOS app I'm using GoogleMaps and APIs for maps and I'm not very familiar with it.
I know that with iOS native MKMapView you can set a visible map rect to fit all the points needed at a proper zoom level, like described here: Zooming MKMapView to fit annotation pins?
but I'm not sure how to do such thing for GMSMapView. Does anyone know how to do it?
Any kind of help is highly appreciated!

Add this after setting up markers on the Map:
var bounds = GMSCoordinateBounds()
for coordinate in testCoordinates {
bounds = bounds.includingCoordinate(coordinate)
}
let update = GMSCameraUpdate.fit(bounds, withPadding: 50)
mapView.animate(with: update)

Related

How to get a reliable rect of the whole mapview (not just visible) with Mapbox?

So I've been struggling with this for some time. I wish to know the CGRect of the whole map. The issue I am facing right now is that due to the wrapping of the map, the x of the top-left coordinate keeps switching between a negative and a positive value as I move around the map. This results in the width of the map also changing.
I tried restricting the camera bounds to prevent wrapping and it does that job perfectly, but it still doesn't help with x switching between positive/negative as I move around the map.
let extremeBounds = CoordinateBounds(
southwest: CLLocationCoordinate2D(latitude: -62.59334083012023, longitude: -141.328125),
northeast: CLLocationCoordinate2D(latitude: 82.85338229176081, longitude: 167.34375)
)
try? view.mapboxMap.setCameraBounds(with: CameraBoundsOptions(bounds: extremeBounds))
This is how I am calculating the rect on cameraChange:
mapView.mapboxMap.rect(for: self.extremeBounds)
Is there an official way of disabling the wrapping of the map so that the northwest coordinate always appears at the top left side of the map? Or maybe my approach is incorrect and there's an easier way of finding out the rect of the map?
you can try setting ConstrainModeto widthAndHeight, by default this value is heightOnly-> only y axis is constrained.
let mapInitOptions = MapInitOptions(mapOptions: MapOptions(constrainMode: .widthAndHeight))
let mapView = MapView(frame: view.bounds, mapInitOptions: mapInitOptions)

How do i restrict panning and restrict zooming out of a certain area of apple maps in MapKit?

I am currently making an app with a map that should focus on a certain location only. I would like the user to not be able to zoom out or pan out of this area so they can keep their focus on the image overlay that i have put over this area.
In order to get the app to start off from the location that i want and not some random map, I used a tutorial from Ray Wenderlich: https://www.raywenderlich.com/425-mapkit-tutorial-overlay-views
How would I acoomplish my task based on the code that is written in the tutorial above? I have completed the tutorial, so I am looking for help in adding any code and identifying where and what kind of code to put.
I found other tutorials on this topic unhelpful because they were for other map types like Google maps or MapBox. The apple website about MapKit and MaximumZ does not help me very much either.
I am a beginner in XCode and Swift, and have only had little bit of experience in Python previously. I was hoping limiting the zoom and user access to parts of the maps would be easier...
override func viewDidLoad() {
super.viewDidLoad()
let latDelta = park.overlayTopLeftCoordinate.latitude -
park.overlayBottomRightCoordinate.latitude
// Think of a span as a tv size, measure from one corner to another
let span = MKCoordinateSpanMake(fabs(latDelta), 0.0)
let region = MKCoordinateRegionMake(park.midCoordinate, span)
mapView.region = region
}
This is what I have so far for getting the app to startup on the location that I want, using a rectangle that bounds the area that I am looking to restrict the user to.
The MKMapView has a delegate MKMapViewDelegate. This protocol has a function called:
func mapViewDidChangeVisibleRegion(_:)
This method is called whenever the user scrolls or zooms the map. In this method you can specify the behavior of the map. You can, for instance set a specific region that you want the map to zoom into and specify the maximum level of zoom allowed.
In the function mapViewDidChangeVisibleRegion(_:) you can then check to what latitudeDelta and longitudeDelta the map can zoom into. If the delta's go below or above a certain level you can lock the zooming by setting the region with something like this:
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
if mapView.region.span.latitudeDelta < 0.4 {
regionSpan = MKCoordinateSpan(latitudeDelta: 0.4, longitudeDelta: 0.5)
let mapRegion = MKCoordinateRegion(center: self.centerCoordinate, span: self.regionSpan)
self.trackMapView.setRegion(mapRegion, animated: true)
}
}

how to fix boundaries to show limited google map area in Swift 4 (iOS)

I am able to set limited area to show at google map for user. After setting below code google map is not moving outside of this area defined with below code in Android: -
final LatLngBounds ADELAIDE = new LatLngBounds(
new LatLng(25.27, 93.46), new LatLng(27.05, 95.19));
// Constrain the camera target to the Adelaide bounds.
mMap.setMinZoomPreference(6.0f);
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.05839, 94.45399), 6.0f));
Now I am trying to implement same feature to restrict user for google map area in Swift but there is no method like setLatLngBoundsForCameraTarget.
I have tried below code: -
let southWest = CLLocationCoordinate2DMake(25.27,93.46)
let northEast = CLLocationCoordinate2DMake(27.05,95.19)
let bounds = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let cameraBounds = googleMapView.camera(for: bounds, insets: UIEdgeInsets.zero)
googleMapView.camera = cameraBounds!
but this is not working. User still can move outside of this bounds.
Please help. Thanks in advance.

How to move annotation with animation in mapbox map?

I am using mapbox map in my iOS app. I added annotation in mapbox map by using this class MGLPointAnnotation.
Now the problem is i am unable to change its location. This is how i changing annotation location. But it is not moving. I need to remove first old one and again need to add new annotation but i don't want to do this.Please let me know how can i do this.
car.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lng)
I want the movement of annotation when location changed of annotation with smooth animation.
Also when there is a change in location i'm changing center of my mapbox map with camera. Let me know i am doing right or not.
// Optionally set a starting point.
bMbMapView.setCenter(cord, zoomLevel: 7, direction: bearing, animated: true)
bMbMapView.setDirection(bearing, animated: true)
// Create a camera that rotates around the same center point, rotating 180°.
// `fromDistance:` is meters above mean sea level that an eye would have to be in order to see what the map view is showing.
let camera = MGLMapCamera(lookingAtCenter: cord, fromDistance: 1000, pitch: 7, heading: 90)
// Animate the camera movement over 5 seconds.
bMbMapView.setCamera(camera, withDuration: 1, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
A MGLAnnotationView is a UIView, so you should be able to animate changes with Core Animation: https://github.com/mapbox/mapbox-gl-native/issues/8378#issuecomment-287910768
Another option is to use a style source and layer, then update the source using a timer. This would likely be less smooth than using an animated annotation view.

Skobbler iOS SDK: Why are there borders around my map annotations?

Creating an iOS mapping app using Skobbler's SDK (installed via CocoaPods. v2.5). I'm adding annotations to my Skobbler mapview, using custom views for my pins instead of using one of Skobbler's defaults. My pins will just be colored circles.
The problem is that my circles seem to be drawn with borders, but they shouldn't be.
My code is similar to the following:
let pin = UIView()
pin.layer.frame = CGRectMake(0, 0, 18, 18)
pin.layer.backgroundColor = UIColor.lightGrayColor().CGColor
pin.layer.cornerRadius = diameter/2
//Just for good measure... NO BORDER
pin.layer.borderColor = UIColor.clearColor().CGColor
pin.layer.borderWidth = 0
let annotation = SKAnnotation()
annotation.annotationView = SKAnnotationView(view: pin, reuseIdentifier: "viewID")
annotation.identifier = 1234
annotation.location = CLLocationCoordinate2DMake(44.1234, -111.1234)
mapView.addAnnotation(annotation, withAnimationSettings: animationSettings)
How can I avoid having borders drawn around my pins?
Turns out that a light border is added by the framework, and I'm told there is nothing I can do.
Here's a snippet from an email from Skobbler:
"Indeed we can see a light border (see attach)- that’s the way is
generated at our core level and we cannot change this. We’ve checked
also if we set a simple view on the map and if we add that view to an
annotation indeed the border appears (screenshot 2)."

Resources