gpt4 book ai didi

swift - 按下按钮 Centers\Focuses MKmapView 到用户的位置,Swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:29 24 4
gpt4 key购买 nike

我的代码:

拜托任何人!如何让这个按钮起作用!?!??!

我要在@IBAction 括号中放什么来实现我想要的功能?

    import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{



@IBOutlet weak var mapView: MKMapView!



let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

//==========RegionLocation : =========

// Init the zoom level
let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
let span = MKCoordinateSpanMake(125, 125)
let region = MKCoordinateRegionMake(coordinate, span)
self.mapView.setRegion(region, animated: true)


//====================================\\
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of resources that can be re created.

}

//Mark : Location

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])



{
let location = locations.last

let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))

self.mapView.setRegion(region, animated: true)

self.locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
pinView?.leftCalloutAccessoryView = button
}
else
{
pinView!.annotation = annotation
}


return pinView

}

但是当我放入按钮操作部分时,当我按下它时没有任何反应?

任何人都可以指导我如何将 mapView 居中到用户位置蓝点?

最佳答案

试试这个

@IBAction func centerLocationButton(sender: UIButton) {
mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}

关于swift - 按下按钮 Centers\Focuses MKmapView 到用户的位置,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com