【问题标题】:setCoordinate updating after annotation added on my mapView在我的 mapView 上添加注释后 setCoordinate 更新
【发布时间】:2012-12-03 16:51:12
【问题描述】:

我添加了一堆注释,并将它们放在一个数组中,这样我就可以拉入它们的新位置并相应地更新地图,而无需先删除所有注释并添加它们会导致闪烁。不幸的是,在最初设置并添加它们的坐标后,任何 setCoordinate 调用都不再有效。有什么想法吗?

- (void)updateMap:(NSData *)responseData {
    //parse out the JSON data
    NSError* error;
    vehicleData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    //find which Dictionary is for our bus.
    for (NSDictionary* route in vehicleData) {
        //find our bus in our vehicles dictionary. Key is by routeID.
        BusPositionDot *busLocationDot; 
        if ([vehicles objectForKey:[route objectForKey:@"RouteID"]] == nil) {
            busLocationDot = [[BusPositionDot alloc] init];
            [vehicles setObject:busLocationDot forKey:[route objectForKey:@"RouteID"]];
            [mapView addAnnotation:[vehicles objectForKey:[route objectForKey:@"RouteID"]]];

        }
        else {
            busLocationDot = [vehicles objectForKey:@"RouteID"];
        }

        float latitude = [[route objectForKey:@"Latitude"] floatValue];
        float longitude = [[route objectForKey:@"Longitude"] floatValue];
        float groundSpeed = [[route objectForKey:@"GroundSpeed"] floatValue];
        float direction = [[route objectForKey:@"Heading"] floatValue];

        float roundedDirection=45 * round(direction/45);

        if(groundSpeed<=3)
            //get view for annotation
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot.png"];
        else if((roundedDirection==0)||(roundedDirection==360))
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot0.png"];
        else if(roundedDirection==45)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot45.png"];
        else if(roundedDirection==90)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot90.png"];
        else if(roundedDirection==135)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot135.png"];
        else if(roundedDirection==180)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot180.png"];
        else if(roundedDirection==225)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot225.png"];
        else if(roundedDirection==270)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot270.png"];
        else if(roundedDirection==315)
            [mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot315.png"];

        CLLocationCoordinate2D currentBusLocation = CLLocationCoordinate2DMake(latitude, longitude);
        NSLog(@"setting coord %f & %f",currentBusLocation.latitude,currentBusLocation.longitude);

        [UIView animateWithDuration:0.5 animations:^{
            [busLocationDot setCoordinate:currentBusLocation];

        }];
    }
}

【问题讨论】:

  • 当你调用addAnnotation时,你传递[vehicles objectForKey:[route objectForKey:@"RouteID"]],但如果它已经存在,你使用[vehicles objectForKey:@"RouteID"]。这是为什么?当setCoordinate 被调用(无、崩溃、其他)时究竟会发生什么?

标签: ios mkmapview mkannotation mkannotationview


【解决方案1】:

参考这部分:

if ([vehicles objectForKey:[route objectForKey:@"RouteID"]] == nil) {
    ...
    [mapView addAnnotation:
        [vehicles objectForKey:[route objectForKey:@"RouteID"]]];
}
else {
    busLocationDot = [vehicles objectForKey:@"RouteID"];
}

当你调用addAnnotation时,你通过了:

[vehicles objectForKey:[route objectForKey:@"RouteID"]]

但如果它已经存在,则使用:

[vehicles objectForKey:@"RouteID"]

这意味着在更新注释时,使用了不同的(很可能是错误的)引用。
[vehicles objectForKey:@"RouteID"] 实际上不是 BusPositionDot,或者它不是最初使用该“路由 id”添加的同一实例。

因此,setCoordinate 不起作用。
更新注释时使用相同的引用应该可以修复它。



还有一些其他不相关的潜在问题:

  • 代码正在使用float 变量(例如if(roundedDirection==45))进行直接比较。即使它“似乎有效”也不推荐这样做——存在浮点精度错误的可能性。检查roundedDirection 是否在目标值的很小范围内,或者在您的情况下,只需将roundedDirection 声明为int,因为它看起来像表达式
    45 * round(direction/45) 只会返回没有的值分数。

  • 代码是直接设置注解视图的image。这没关系,但请确保viewForAnnnotation 委托方法也具有相同的确切逻辑来根据方向设置image,否则可能会发生注释视图的图像在平移或缩放时将重置为某个默认值。您可能需要将direction 属性添加到BusPositionDot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多