【问题标题】:Zoom in on User Location- Swift放大用户位置 - Swift
【发布时间】:2015-09-11 11:43:30
【问题描述】:

我不知道如何让地图从 viewDidLoad 放大用户位置。我尝试设置一个区域,但没有奏效。这是我的代码,有什么提示吗?

@IBOutlet 弱 var mapView: MKMapView! var MapViewLocationManager:CLLocationManager! = CLLocationManager() var currentLocation:PFGeoPoint! = PFGeoPoint()

override func viewDidLoad() {
    super.viewDidLoad()
    self.mapView.showsUserLocation = true
    mapView.delegate = self
   MapViewLocationManager.delegate = self
    mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}

我已经查找了这个问题的答案,但没有在 Swift 中找到答案,也没有找到真正有效的答案。谢谢!!

【问题讨论】:

    标签: ios swift mapkit


    【解决方案1】:

    我会尝试这样的:

        let latitude:CLLocationDegrees = //insert latitutde
    
        let longitude:CLLocationDegrees = //insert longitude
    
        let latDelta:CLLocationDegrees = 0.05
    
        let lonDelta:CLLocationDegrees = 0.05
    
        let span = MKCoordinateSpanMake(latDelta, lonDelta)
    
        let location = CLLocationCoordinate2DMake(latitude, longitude)
    
        let region = MKCoordinateRegionMake(location, span)
    
        mapView.setRegion(region, animated: false)
    

    我看到你说你尝试设置一个区域。也许尝试这样做。

    【讨论】:

    • 非常感谢!我想这就是我卡住的地方,我将如何定义用户位置的纬度和经度?
    • @Caitlin 不客气!我在手机上,所以我无法解释,但如果视频没有帮助,请查看:youtu.be/pSjbMwR2A2U 和此:stackoverflow.com/a/25698536
    【解决方案2】:

    在大多数应用中,都实现了“当前位置”按钮。一个简单的方法是这样的:

    @IBAction func myLocationButtonTapped(_ sender: Any) {
    
        mapView.showsUserLocation = true
        mapView.setUserTrackingMode(.follow, animated: true)
    
    }
    

    一旦用户平移或缩放地图,跟踪就会停止。所以对我来说已经足够了。

    【讨论】:

      【解决方案3】:

      你必须试试这个。 它会缩放地图。

      let span = MKCoordinateSpanMake(0.050, 0.050)
      let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 23.0225, longitude: 72.5714), span: span)
      mapView.setRegion(region, animated: true)`
      

      【讨论】:

      • 如果您想放大更多,只需在垃圾邮件中使用较小的数字:let spam = MKCoordinateSpanMake(0.001, 0.001)
      【解决方案4】:
      @IBOutlet weak var conscienceMap: MKMapView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
          conscienceMap.showsUserLocation = true
          conscienceMap.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)
      }
      

      【讨论】:

      • 请说明您的代码。仅包含代码的答案并不能很好地帮助他人学习。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2015-04-02
      相关资源
      最近更新 更多