如何制作一个将视图集中在用户位置的按钮和另一个将视图集中在特定坐标的按钮?
我需要制作一个按钮,将地图以用户位置为中心,另一个按钮将地图以特定坐标为中心(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 技术交流群。

您已经编写了一个函数
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'sIBAction
. 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 functioncenterMapOnCoordinate(_ coordinate: CLLocationCoordinate2D)
and have both of your buttonIBAction
s call that function, passing in the appropriate coordinate in each case.