gpt4 book ai didi

xcode - 如何使用 UILongPressGestureRecognizer 在用户位置上添加图钉?

转载 作者:行者123 更新时间:2023-11-30 14:06:03 26 4
gpt4 key购买 nike

我正在制作一个在 map 上添加图钉的应用程序,到目前为止,它一直在用户长按的位置添加图钉,但我希望它进行更改并将其添加到用户位置上,但是,我不知道要更改什么才能实现它,您能更正我的代码以使其工作吗?谢谢,我在下面添加了我的代码:

var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")

uilpgr.minimumPressDuration = 2.0

Map.addGestureRecognizer(uilpgr)

}

func action(gestureRecognizer:UIGestureRecognizer) {

if gestureRecognizer.state == UIGestureRecognizerState.Began {

var touchPoint = gestureRecognizer.locationInView(self.Map)

var newCoordinate = self.Map.convertPoint(touchPoint, toCoordinateFromView: self.Map)

var location = CLLocation(latitude: newCoordinate.latitude , longitude: newCoordinate.longitude)

这是我正在使用的用户位置代码:

 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

var userLocation:CLLocation = locations[0] as! CLLocation

var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude

var coordinate = CLLocationCoordinate2DMake(latitude, longitude)

var latDelta:CLLocationDegrees = 0.01
var lonDelta:CLLocationDegrees = 0.01

var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)

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


}

最佳答案

当您长按被触发时,只需向您的 map 添加一个注释对象,该对象是一个轻量级模型对象,与您的图钉将保存的数据相对应。

let annotation = MKAnnotation();
annotation.title = "My location";
annotation.coordinate = self.Map.userLocation.location.coordinate;
self.Map.addAnnotation(annotation);

添加注释后,请确保您已注册到 map View 的委托(delegate),它会要求您为添加的注释提供 View 。

func mapView(_ mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!{
if(annotation is MKUserLocation){
return nil;
}
let pinView = mapView.dequeueReusableAnnotationForIdentifier("MyIdentifier");
let pinAnnotationView = MKPinAnnotationView(annotation,reuseIdentifier:"MyIdentifier");
return pinAnnotationView;
}

我确信存在一些语法问题,但希望您理解其中的逻辑。创建用户位置的注释。这会触发 map View 的协议(protocol)方法,并在其中提供带有附加注释的引脚注释 View 。

关于xcode - 如何使用 UILongPressGestureRecognizer 在用户位置上添加图钉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32379994/

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