【问题标题】:How to update Mapview Periodically?如何定期更新 Mapview?
【发布时间】:2011-10-17 15:12:07
【问题描述】:

在我的应用程序中,我想定期更新 MapView 并根据新位置重绘地图上的路径。 是否可以使用 MKMapView?

这是我在 mapview 上绘制和注释的方法。

- (void)viewDidLoad
 {

NSString *url=[NSString stringWithFormat:@"xxxxx.php?uid=%@&buddy_uid=%@",UserUID,buddyUid];
NSLog(@"%@",url);
NSLog(@"%@",url);
NSString * jsonString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"return data %@",jsonString);
NSDictionary *jsonDic = [jsonString JSONValue];
buddyLocation = [[NSString alloc] initWithFormat:@"%@,%@",[jsonDic objectForKey:@"Lattitude"],[jsonDic objectForKey:@"Longitude"]];

[super viewDidLoad];

locmanager = [[CLLocationManager alloc] init]; 
[locmanager setDelegate:self]; 
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
//[locmanager startUpdatingLocation];
MKMapView *mapnew;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    mapnew = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
}
else
{
    mapnew = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
}

[mapnew setDelegate:self];
[self.view addSubview:mapnew];

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
region.center.latitude = lat1 floatValue];//22.56574;
region.center.longitude =lng1 floatValue];//73.74575;
NSString *currentLocation = [[NSString alloc] initWithFormat:@"%@,%@",lat1,lng1];
NSLog(@"%@",currentLocation);

DisplayMap *ann = [[DisplayMap alloc] init]; 
ann.title = @"Current Location";
ann.coordinate = region.center; 
[mapnew addAnnotation:ann];

NSLog(@"%@",jsonDic);
region.center.latitude = [[jsonDic objectForKey:@"Lattitude"] floatValue];//23.56574;
region.center.longitude = [[jsonDic objectForKey:@"Longitude"] floatValue];//72.74575;

ann = [[DisplayMap alloc] init]; 
ann.title = @"Buddy Location";
ann.coordinate = region.center; 

    [mapnew addAnnotation:ann];
    KMLParser *kml = [KMLParser parseKMLAtURL:[NSURL URLWithString:[NSString 
    stringWithFormat:@"https://maps.google.com/maps?saddr=%@&daddr=%@&
    output=kml",currentLocation,buddyLocation]]]; 
    NSArray *overlays = [kml overlays];
    [mapnew addOverlays:overlays];

    MKMapRect flyTo = MKMapRectNull;
    for (id <MKOverlay> overlay in overlays) 
    {
        if (MKMapRectIsNull(flyTo)) 
        {
            flyTo = [overlay boundingMapRect];
        }
        else 
        {
            flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
        }
    }

    mapnew.visibleMapRect = flyTo;
    [mapnew release];
    mapnew =nil;
}

 }
 -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
 {        
MKPolylineView *line = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease]; 
line.strokeColor = [UIColor blueColor]; 
line.lineWidth =2.0;
return line;
 }

【问题讨论】:

  • 有点难以理解你想要做什么。您应该扩展您的问题并添加一些示例。
  • 我想跟踪我名单上的人。在 Mapview 中,我显示了我当前位置与朋友位置之间的路径和引脚。所以当我加载视图时它只是第一次绘制。但我想在特定时间更新 MapView 上的路径。

标签: iphone android-mapview


【解决方案1】:

ViewDidiLoad 方法仅在视图加载时调用一次,之后它只会在连续访问时将控制权交给 viewWillappear 等方法。您需要做的是将解析位置数据的方法与 viewDidLoad 分开,并使其设置一个数组或对象。并调用该方法在 Map 上重置这些内容,即删除以前的注释或覆盖,然后添加新的。问题解决了。您可以使用计时器或类似的东西以固定的时间间隔调用这些方法。我想这应该会有所帮助。

【讨论】:

  • 最好将其与接收位置更新联系起来,无论是在 MKMapView 还是 CLLocation 委托方法中,并检查该位置是否与之前的位置相比有很大的变化。
  • 是的,只有当他只检查用户位置时才会有帮助。他还想监视朋友
  • 嗯,他确实提到了他的“当前位置”,应该正在更新。不清楚 php 调用是否正在寻找他朋友的位置;如果是,那么是的,在计时器上调用它。但也应跟踪位置更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
相关资源
最近更新 更多