【问题标题】:swift: Move annotation from one coordinate to anotherswift:将注释从一个坐标移动到另一个坐标
【发布时间】:2014-12-12 22:49:33
【问题描述】:

我一直在尝试将地图上的注释(自定义)从一个坐标移动到另一个坐标,但找不到更好的方法。我在网上搜索过,但找不到任何有用的东西。

有谁成功(迅速)实现了这种功能?

编辑

var annMove: CustomAnnotation = CustomAnnotation();
    annMove.title = "Moving annotation"
    annMove.subtitle = ""
    annMove.imageName = "blue_pin.png"
    self.mapView.addAnnotation(annMove);
    for i in 0..<route.count
    {
        annMove.coordinate = route[i];
    }

自定义注解

class CustomAnnotation: MKPointAnnotation {
var imageName: String!

}

当我运行代码时,我只能看到坐标route[0] 处的注释。如果注释确实移动了,那么至少它应该在坐标route[route.count-1] 而不是route[0]

【问题讨论】:

  • 您是否尝试过设置coordinate 属性?发生了什么?
  • @Mundi 是的,但它不动。我更新了我的帖子,检查一下。
  • 大多数时候,当你想在屏幕上移动某些东西时,移动是由某个事件触发的,例如计时器。如果您只是在 for 循环中设置坐标,它将在代码完成单个运行循环之前将坐标设置为所有位置。坐标变化之间不会更新屏幕。
  • @barksten 是的,但是计时器只是给出了像移动注释一样的动画效果,但最后在路线上的所有坐标点都会有注释。我正在尝试执行类似“在第一个坐标处创建注释,通过坐标数组运行循环,在下一个点创建注释,同时从前一个点删除它(可能是使用提供动画效果的计时器)”之类的操作)”。希望你能理解。

标签: swift annotations mkpointannotation


【解决方案1】:

我希望这可能会有所帮助。它对我有用。

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 latDelta:CLLocationDegrees = 0.05
    var lonDelta: CLLocationDegrees = 0.05

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    self.map.setRegion (region, animated: false)
}

override func viewDidLoad() {
   super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    map.showsUserLocation = true
}

【讨论】:

    【解决方案2】:
    [UIView animateWithDuration:5.0
                                      delay:0.0
                                    options:UIViewAnimationOptionCurveLinear
                                 animations:^{
                                     [CarAnnotationView.annotation setCoordinate:newCoord];
                                 } completion:nil];
    

    【讨论】:

    • 请编辑更多信息。不鼓励使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。我们在这里努力成为知识的资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多