【问题标题】:MKAnnotation not updating positionMKAnnotation 不更新位置
【发布时间】:2013-05-16 21:40:32
【问题描述】:

我正在使用来自 github 的 HGMovingAnnotationHGMovingAnnotationView 代码在 MKmap 上为 MKAnnotation 设置动画。当我从 HG 项目运行示例项目时,一切正常。

我已更改原始 HG 项目,允许我手动将新坐标推送到 HGMapPath,然后将注释移动到我想要的位置。

我已经放置了一个按钮,用于测试,运行手动过程,一切正常。注释在屏幕上移动。问题是,当我现在尝试使用来自实时 socket.io 连接的数据调用此手动方法时,地图注释不会移动。

此外,当地图首次加载时,注释不会显示,直到我稍微移动地图。手动移动注释也是如此,它不会显示数据流中的移动,直到我缩放地图。但是如果我做按钮的方式,避开io流,注释移动而不需要缩放或平移地图?

放置视图注释

if(doubleLat && doubleLng) {

            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(doubleLat, doubleLng);

            //Create path object
            self.assignedAmbPath = [[HGMapPath alloc] initWithCoordinate:coordinate];

            HGMovingAnnotation *movingObject = [[HGMovingAnnotation alloc] initWithMapPath:self.assignedAmbPath];

            self.movingAssignedAmbObject = movingObject;


            // add the annotation to the map
            [self.mapView addAnnotation:movingObject];


            // zoom the map around the moving object
            MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
            MKCoordinateRegion region = MKCoordinateRegionMake(MKCoordinateForMapPoint(self.movingAssignedAmbObject.currentLocation), span);
            [self.mapView setRegion:region animated:YES];

            // start moving the object
            [movingObject start];

        }

有效的代码

 - (IBAction)testMoveBtnPressed:(id)sender {
//TODO: move x and y
DLog(@"============== Test move button was pressed ================ ");
NSLog(@"");

int randn = (random() % 15)+15;
float pscale = (float)randn / 10000;

double lat = 39.9813855 + pscale;
double lng = -75.1502155 + pscale;

for (id<MKAnnotation> annotation in self.mapView.annotations){
    MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
    if (![annotation isKindOfClass:[PhoneAnnotation class]]){
        // Process annotation view
        [((HGMovingAnnotation *)annotation) trackToNewPosition:CLLocationCoordinate2DMake(lat, lng)];
    }
  }
}

不起作用的代码

{

                    //TODO: move thing to new location
                    double doubleLat = [lat doubleValue];
                    double doubleLng = [lng doubleValue];
 //                        NSLog(@"--------------- Jason it is -------------  Latitude   being passed in is %f", doubleLat);
//                        NSLog(@"--------------- Jason it is -------------  Longitude being passed in is %f", doubleLng);
//                        
//                        [self.movingAssignedAmbObject trackToNewPosition:CLLocationCoordinate2DMake(doubleLat, doubleLng)];




                    for (id<MKAnnotation> annotation in self.mapView.annotations){
                        MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
                        if (![annotation isKindOfClass:[PhoneAnnotation class]]){
                            // Process annotation view
                            [((HGMovingAnnotation *)annotation) trackToNewPosition:CLLocationCoordinate2DMake(doubleLat, doubleLng)];

                        }
                    }


                }

【问题讨论】:

  • 你能发布你的代码吗?否则我们将无法为您提供帮助。

标签: ios objective-c mkmapview mkannotation


【解决方案1】:

问题是 HG 库,尽管按照描述的方式工作并不能正常工作,除非您使用路径,如果您不创建附有坐标的注释,但事实并非如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多