如何制作一个将视图集中在用户位置的按钮和另一个将视图集中在特定坐标的按钮?

发布于 01-21 03:23 字数 1901 浏览 3 评论 0原文

我需要制作一个按钮,将地图以用户位置为中心,另一个按钮将地图以特定坐标为中心(centroCU)。当我运行该应用程序时,该按钮显示,但该操作不起作用。我做错了什么?我从 Button to center view to user location using MapKit 获取了代码

 class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    let map = MKMapView()
    let centroCU = CLLocationCoordinate2D(latitude: 25.72714,
                                            longitude: -100.31190)
...

 override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(map)
        map.frame = view.bounds
        
        map.setRegion(MKCoordinateRegion(center: centroCU,
                                         span: MKCoordinateSpan(latitudeDelta: 0.013,
                                                                longitudeDelta: 0.013))
                      , animated: true)
        
        map.delegate = self
        addMapTrackingButton()
... 
    }

 func addMapTrackingButton(){
        let image = UIImage(named: "Recenter") as UIImage?
        let button   = UIButton(type: UIButton.ButtonType.custom) as UIButton
        button.frame = CGRect(origin: CGPoint(x: 315 , y: 50), size: CGSize(width: 45, height: 45))
        button.setImage(image, for: .normal)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(ViewController.centerMapOnCentroCU), for:.touchUpInside)
        map.addSubview(button)
    }
    
    @objc func centerMapOnCentroCU() {
        map.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 25.72714,
                                            longitude: -100.31190),
                                         span: MKCoordinateSpan(latitudeDelta: 0.013,
                                                                longitudeDelta: 0.013))    }
...
}

I need to make a button that centers the map on user's location and another one that centers the map on specific coordinates (centroCU). When I run the app, the button shows, but the action doesn't work. What am I doing wrong? I got the code from Button to center view to user location using MapKit

 class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    let map = MKMapView()
    let centroCU = CLLocationCoordinate2D(latitude: 25.72714,
                                            longitude: -100.31190)
...

 override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(map)
        map.frame = view.bounds
        
        map.setRegion(MKCoordinateRegion(center: centroCU,
                                         span: MKCoordinateSpan(latitudeDelta: 0.013,
                                                                longitudeDelta: 0.013))
                      , animated: true)
        
        map.delegate = self
        addMapTrackingButton()
... 
    }

 func addMapTrackingButton(){
        let image = UIImage(named: "Recenter") as UIImage?
        let button   = UIButton(type: UIButton.ButtonType.custom) as UIButton
        button.frame = CGRect(origin: CGPoint(x: 315 , y: 50), size: CGSize(width: 45, height: 45))
        button.setImage(image, for: .normal)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(ViewController.centerMapOnCentroCU), for:.touchUpInside)
        map.addSubview(button)
    }
    
    @objc func centerMapOnCentroCU() {
        map.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 25.72714,
                                            longitude: -100.31190),
                                         span: MKCoordinateSpan(latitudeDelta: 0.013,
                                                                longitudeDelta: 0.013))    }
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆°2025-01-28 03:23:05

您已经编写了一个函数 centerMapOnCentroCU(),该函数将地图以 centroCU 位置为中心。您需要从按钮的 IBAction 调用该函数。如果它不起作用,请添加断点或打印语句以确保代码正在执行。

顺便说一句,我会将您的 centerMapOnCentroCU() 函数抽象为函数 centerMapOnCooperative(_坐标:CLLocationCooperative2D) 并拥有两个按钮 IBAction 调用该函数,在每种情况下传递适当的坐标。

You already wrote a function centerMapOnCentroCU() that centers your map on the centroCU location. You need to call that function from your button's IBAction. If it isn't working, add a breakpoint or print statement to make sure the code is being executed.

And by the way, I would abstract your centerMapOnCentroCU() function into a function centerMapOnCoordinate(_ coordinate: CLLocationCoordinate2D) and have both of your button IBActions call that function, passing in the appropriate coordinate in each case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文