You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

  • GMSCoordinateBounds is iOS's equivalent to Android's LatLngBounds – it uses the same southwest/northeast coordinate pair to define the restricted area.
  • mapView.minZoom directly sets the minimum allowed zoom level, just like setMinZoomPreference on Android.
  • setCameraTargetBounds(_:animated:) ensures users can't pan the map outside your defined bounds, mirroring the behavior of setLatLngBoundsForCameraTarget.
  • The initial camera position setup and moveCamera call 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:09:08