如何在Swift 4(iOS)中设置谷歌地图边界以限定显示区域
Hey there! I can help you replicate that Google Maps area restriction functionality in Swift 4 for iOS. Here's exactly how to do it, matching your Android implementation step by step:
Swift 4 Google Maps Area Lock Implementation
First, ensure you've got the Google Maps SDK integrated into your iOS project. Then use this code to restrict the map to your desired bounds:
import GoogleMaps // Define your target geographic bounds (matches your Android LatLngBounds) let southwestCoordinate = CLLocationCoordinate2D(latitude: 25.27, longitude: 93.46) let northeastCoordinate = CLLocationCoordinate2D(latitude: 27.05, longitude: 95.19) let adelaideBounds = GMSCoordinateBounds(coordinate: southwestCoordinate, coordinate: northeastCoordinate) // Assume you have a GMSMapView outlet named `mapView` connected in your storyboard/xib // Set minimum zoom preference (equivalent to Android's setMinZoomPreference) mapView.minZoom = 6.0 // Lock camera target to the defined bounds (equivalent to setLatLngBoundsForCameraTarget) mapView.setCameraTargetBounds(adelaideBounds, animated: false) // Move camera to your initial position (matches Android's moveCamera call) let initialCameraPosition = GMSCameraPosition.camera( withLatitude: 26.05839, longitude: 94.45399, zoom: 6.0 ) mapView.moveCamera(GMSCameraUpdate.setCamera(initialCameraPosition))
Key Details Matching Your Android Code:
GMSCoordinateBoundsis iOS's equivalent to Android'sLatLngBounds– it uses the same southwest/northeast coordinate pair to define the restricted area.mapView.minZoomdirectly sets the minimum allowed zoom level, just likesetMinZoomPreferenceon Android.setCameraTargetBounds(_:animated:)ensures users can't pan the map outside your defined bounds, mirroring the behavior ofsetLatLngBoundsForCameraTarget.- The initial camera position setup and
moveCameracall replicate your Android code's starting view perfectly.
If you want the initial camera move to be animated, you can replace mapView.moveCamera(...) with mapView.animate(to: initialCameraPosition).
内容的提问来源于stack exchange,提问作者Asif Raza
相关产品推荐
相关产品推荐

