Ios 当用户滚动地图时,将pin(MKAnnotation)保持在中间
我希望在用户使用类似地图的Careem应用程序时,将mkannotation保持在屏幕中央: 到目前为止,我设法显示了pin,更新了pin的位置,但当我滚动地图时,代码中的注释最初会移动,然后回到中心。我希望别针保持在中间Ios 当用户滚动地图时,将pin(MKAnnotation)保持在中间,ios,swift,mkmapview,mkannotation,Ios,Swift,Mkmapview,Mkannotation,我希望在用户使用类似地图的Careem应用程序时,将mkannotation保持在屏幕中央: 到目前为止,我设法显示了pin,更新了pin的位置,但当我滚动地图时,代码中的注释最初会移动,然后回到中心。我希望别针保持在中间 @IBOutlet var mapView: MKMapView! var centerAnnotation = MKPointAnnotation() var manager:CLLocationManager! override func viewDid
@IBOutlet var mapView: MKMapView!
var centerAnnotation = MKPointAnnotation()
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager() //instantiate
manager.delegate = self // set the delegate
manager.desiredAccuracy = kCLLocationAccuracyBest // required accurancy
manager.requestWhenInUseAuthorization() // request authorization
manager.startUpdatingLocation() //update location
var lat = manager.location.coordinate.latitude // get lat
var long = manager.location.coordinate.longitude // get long
var coordinate = CLLocationCoordinate2DMake(lat, long)// set coordinate
var latDelta:CLLocationDegrees = 0.01 // set delta
var longDelta:CLLocationDegrees = 0.01 // set long
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
self.mapView.setRegion(region, animated: true)
centerAnnotation.coordinate = mapView.centerCoordinate
self.mapView.addAnnotation(centerAnnotation)
}
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
centerAnnotation.coordinate = mapView.centerCoordinate;
}
****更新****
根据安娜的建议,我在地图上添加了一个视图。代码如下:
var newPoint = self.mapView.convertCoordinate(mapView.centerCoordinate, toPointToView: self.view)
var pinImage = UIImage(named: "yoga_pins.png")
var imageView = UIImageView(image: pinImage) // set as you want
imageView.image = pinImage
imageView.backgroundColor = UIColor.clearColor()
imageView.contentMode = UIViewContentMode.Center
imageView.center.y = newPoint.y
imageView.center.x = newPoint.x
self.view.addSubview(imageView)
唯一的问题是,当第一次加载地图时,位于mapView.centerCoordinate
上的注释将用于获取纬度和经度:
然后,当我滚动地图时,图钉会移动到正确的位置(在图像下方):
您可以使用自定义按钮。把那个按钮加在地图中央。只需根据您的条件显示和隐藏该按钮。 所以,当你们移动地图时,按钮保持在地图中心的相同位置 我推荐你 它是一个自定义地图视图,具有动画和可自定义的中心销,用于在地图中选择位置 您应该安装Pod,然后,作为问题的解决方案,您应该实现委托,以便您可以获得引脚掉落的位置
pinMapView.delegate=self
扩展名MyViewController:DSCenterPinMapViewDelegate{
func DidStartDraging(){
//我的习惯行为
}
func didendraging(){
//我的习惯行为
selectedLocation=pinMapView.mapview.centerCoordinate
}
}
不要使用实际的注释。要获得平滑、浮动的效果,请参见此方法:@Anna谢谢。做这件事的好方法。我可能在寻找错误的问题/答案。@Anna你能看看我更新的代码,看看我做错了什么吗?谢谢
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
centerAnnotation.coordinate = mapView.centerCoordinate;
}