[英]How do I set a pin on Current Location, use Pin as Geofence, alert user onEnter, then reset all / start over again?
[英]How can I use a UILongPressGestureRecognizer to add pin on user location?
我正在制作一個在地圖上添加圖釘的應用程序,到目前為止,它一直在用戶長按的位置添加圖釘,但是我希望它可以更改並添加到用戶位置,但是,我不知道不知道要進行哪些更改才能使其實現,請您更正我的代碼以使其生效嗎? 謝謝,我在下面添加了我的代碼:
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;
}
我敢肯定有一些語法問題,但希望您理解邏輯。 為用戶的位置創建注釋。 這將觸發您的地圖視圖的協議方法,並在其中提供帶有附加注釋的引腳注釋視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.