我正在制作一个在地图上添加图钉的应用,到目前为止,它一直在用户长按的位置添加图钉,但是我希望它可以更改并添加到用户位置,但是,我不知道不知道要进行哪些更改才能使其实现,请您更正我的代码以使其起作用吗?谢谢,我在下面添加了我的代码:
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)
}
触发长按时,只需在地图上添加一个注释对象即可,它是轻量级的模型对象,与您的图钉将保存的数据相对应。
let annotation = MKAnnotation();
annotation.title = "My location";
annotation.coordinate = self.Map.userLocation.location.coordinate;
self.Map.addAnnotation(annotation);
添加注释后,请确保您已注册到地图视图的委托,并且它将要求您为添加的注释提供视图。
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;
}
我确定确实存在语法问题,但希望您能理解其中的逻辑。为用户的位置创建注释。这将触发您的地图视图的协议方法,并在其中提供带有附加注释的引脚注释视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句