Issue
In my application i want to update the MapView periodically and redraw the the path on map as per new location. Is it possible using MKMapView?
This is my didload metho for drawing and annotation on 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;
}
Solution
ViewDidiLoad method is called only once when the view is loaded After that it would only give control to methods like viewWillappear etc on successive visits . What you have to do is seperate the method which parses the location data from viewDidLoad, and make it set an array or objects . And call the method to reset these contents on Map i.e. remove previous annotations or overlay and then add new ones . problem solved. You could call these method at fixed interval using timer or something like that. I guess this should help.
Answered By - J S Rodrigues
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.