我在这里有一个基本设置的地图,我试图让引脚动画一滴...就像你按下&按住地图应用程序并将图钉放到该位置。因此,当我进入视图时,所有引脚都会动画到它们的位置,我真的不知道该怎么做。如果有人能引导我使用正确的代码,我需要添加它将会很棒!
以下是我现有的代码:
class FirstViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
// Drop a pin
let Litzman = MKPointAnnotation()
Litzman.coordinate = LitzmanLocation
Litzman.title = "Litzman Bar"
Litzman.subtitle = "Nemal Tel Aviv St'"
mapView.addAnnotation(Litzman)}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of resources that can be re created.
}
//Mark:
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.1, longitudeDelta: 0.1))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
答案 0 :(得分:8)
在此之前,我没有维持任何标准,我只有一个解决方案,我正在通过解决其他问题来学习 swift 和 mapKit 。
这里我给出了一些示例代码,并通过对象库添加了 mapView 和 UITapGestureRecognizer 。
用于添加注释,例如从顶部删除
pinView!.animatesDrop = true
viewForAnnotation 委派方法中的。当您在地图上点按时, addAnnotation 方法会调用以添加注释。
用于设置所有注释的动画
我已经实施了 regionDidChangeAnimated 委托方法,只需删除并重新添加所有注释。
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
self.mapView.delegate = self
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
let locationData = [
//Walker Art Gallery
["name": "Walker Art Gallery",
"latitude": 12,
"longitude": 79],
//Liver Buildings
["name": "Liver Buildings",
"latitude": 13,
"longitude": 77],
//St George's Hall
["name": "St George's Hall",
"latitude": 14,
"longitude": 77]
]
var annotations = [MKPointAnnotation]()
for each in locationData {
let latitude = CLLocationDegrees(each["latitude"] as! Double)
let longitude = CLLocationDegrees(each["longitude"] as! Double)
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let name = each["name"] as! String
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "\(name)"
annotations.append(annotation)
}
mapView.addAnnotations(annotations)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
print("viewForAnnotation \(annotation.title)")
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
}
return pinView
}
@IBAction func addAnnotation(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
let location = sender.locationInView(mapView)
let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print("regionDidChangeAnimated")
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
}
修改强>
对于viewDidLoad方法中的情况,请添加此
self.mapView.delegate = self
要注释MKAnnotation,您必须实现以下委托方法
//Annotation view
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
}
return pinView
}
在注释所有注释之后,您必须在委托方法下面实现。
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
我假设您知道如何在地图上添加注释...
<强> EDIT2 强>
这就是你需要我的朋友。
在viewDidLoad中添加一个通知观察者,如下所示。
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)
最后将 regionDidChangeAnimated 更改为 didBecomeActive ,如下所示
func didBecomeActive() {
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
答案 1 :(得分:0)
在目标C中:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
pinView.pinTintColor = [UIColor blueColor];
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}