2015-12-10 3 views
1

У меня есть два ответа от сервера (долгота и широта). Этот ответ сохраняется и отображается в ячейке таблицы. При щелчке по ячейке есть карта с указанием местоположения сервера. Как я могу обновить местоположение карты (переместить маркер) в зависимости от ответа сервера?Udpating Расположение карты в iOS

Вот мой код на карте:

@interface ChildDetailViewController()

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 

@end 

@implementation ChildDetailViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.mapView.delegate = self; 
    // Do any additional setup after loading the view. 

    MapAnnotation *mapPoint = [[MapAnnotation alloc] init]; 
    mapPoint.coordinate = CLLocationCoordinate2DMake([self.appDelagate.latitude doubleValue], [self.appDelagate.longitude doubleValue]); 
    mapPoint.title = self.appDelagate.name; 
    mapPoint.mapimage = self.appDelagate.image; 

    // Add it to the map view 
    [self.mapView addAnnotation:mapPoint]; 

    // Zoom to a region around the pin 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500); 

    [self.mapView setRegion:region]; 


} 


#pragma mark - MKMapViewDelegate 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *view = nil; 
    static NSString *reuseIdentifier = @"MapAnnotation"; 

    // Return a MKPinAnnotationView with a simple accessory button 

    view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier]; 
    if(!view) 
    { 
     view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
     view.canShowCallout = YES; 
     view.animatesDrop = YES; 
    } 

    return view; 
} 

Вот код в ячейки таблицы:

#import "ChildListViewController.h" 
#import "AppDelegate.h" 
#import "Child.h" 
#import "ChildDetailViewController.h" 


@interface ChildListViewController() 
{ 
    NSArray *_child_info; 
    //NSString *locationName; 
} 


@end 

@implementation ChildListViewController 


- (IBAction)unwindchild:(UIStoryboardSegue *)segue 
{ 
    } 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 



    NSLog(@"Pumasok ba?"); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"TableIdentifier"; 

    AppDelegate *appDelegate = [_child_info objectAtIndex:indexPath.row]; 



    Child *cell = (Child *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Child" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    cell.childName.text = appDelegate.name; 
    cell.childbeaconID.text = appDelegate.beacon_id; 
    cell.childstatus.text = appDelegate.status; 
    cell.childLoclong.text = appDelegate.longitude; 
    cell.childLoclat.text = appDelegate.latitude; 
    cell.serverCreatedDate.text = appDelegate.server_created_date; 
    cell.childimageview.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: 
                 [NSURL URLWithString:appDelegate.image]]]; 

return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [_child_info count]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     return 119; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    if (appDelegate.name != nil) 
    { 
     [self performSegueWithIdentifier:(@"a") sender:(self)]; 
    } 
    else 
    { 

     NSLog(@"No Data %@", appDelegate.name); 
    } 
} 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier]isEqualToString:@"a"]) 
    { 
      ChildDetailViewController *vc = segue.destinationViewController; 
      NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
      vc.appDelagate = [_child_info objectAtIndex:indexPath.row]; 

    } 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

Разбираем ответ сервера AppDelegate.m

NSDictionary *fetchedDictionary =[userPushInfo objectForKey:@"aps"]; 
      NSDictionary *fetchedDictionaryalert = [fetchedDictionary objectForKey:@"alert"]; 
      NSDictionary *fetchedDictionarybody = [fetchedDictionaryalert objectForKey:@"body"]; 
      NSDictionary *fetchedDictionaryresult = [fetchedDictionarybody objectForKey:@"student_event"]; 
      for (NSDictionary *user in fetchedDictionaryresult) 
      { 

        beacon_device_name =[user objectForKey:@"beacon_device_name"]; 
        image =[user objectForKey:@"image"]; 
        latitude = [user objectForKey:@"latitude"]; 
        longitude =[user objectForKey:@"longitude"]; 
        server_created_date = [user objectForKey:@"server_created_date"]; 
        status =[user objectForKey:@"status"]; 
      } 

ответ

0

вы можете обновить, делая это после того, как вы получите ответ

[self.mapView removeAnnotations:self.mapView.annotations]; 

MapAnnotation *mapPoint = [[MapAnnotation alloc] init]; 
mapPoint.coordinate = CLLocationCoordinate2DMake([newLatitude doubleValue], [newLongitude doubleValue]); 
mapPoint.title = self.appDelagate.name; 
mapPoint.mapimage = self.appDelagate.image; 

// Add it to the map view 
[self.mapView addAnnotation:mapPoint]; 

// Zoom to a region around the pin 
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500); 

[self.mapView setRegion:region]; 

вы можете общаться в контроллерах просмотреть с помощью Notification Center, поставил первый в контроллере зрения ребенка, а второй после разбора вашего ответа.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLocation) name:AppDelegateReceiveTheLocation object:nil]; 
[[NSNotificationCenter defaultCenter] postNotificationName:AppDelegateReceiveTheLocation object:nil userInfo:nil]; 
+0

Сэр, маркер карты не перемещается. Это не обновление sir –

+0

Вы могли бы показать код, как разобрать и сохранить местоположение в ответе –

+0

Сэр, я не могу отправить код, чтобы добавить комментарий –

Смежные вопросы