MKAnnotation不更新位置

时间:2013-05-16 21:40:32

标签: ios objective-c mkmapview mkannotation

我正在使用github上的HGMovingAnnotationHGMovingAnnotationView代码为MKAnnotation上的MKmap设置动画。当我从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)];

                        }
                    }


                }

1 个答案:

答案 0 :(得分:0)

问题是HG库虽然按照描述的方式工作不正常,除非你使用路径,如果你没有创建附加坐标的注释,它不会。