在我的应用程序中,我想定期更新MapView并根据新位置重新绘制地图上的路径。 是否可以使用MKMapView?
这是我在mapview上绘制和注释的didload方法。
- (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;
}
答案 0 :(得分:0)
ViewDidiLoad方法只在加载视图时调用一次之后,它只会在连续访问时控制viewWillappear等方法。您需要做的是分离从viewDidLoad解析位置数据的方法,并使其设置一个或多个数组。并调用方法在Map上重置这些内容,即删除以前的注释或叠加,然后添加新的注释。问题解决了。您可以使用计时器或类似的东西以固定间隔调用这些方法。我想这应该会有所帮助。