我有两个来自服务器的响应(经度和纬度)。此响应将保存并显示到表格单元格。单击单元格时,会有一个地图,其中包含服务器指定的位置。如何更新地图的位置(移动标记)取决于服务器给定的响应?
这是我在地图中的代码:
@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 :(得分: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];
您可以使用通知中心在视图控制器中进行通信,将第一个放在子视图控制器中,然后在解析响应后进行第二次。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLocation) name:AppDelegateReceiveTheLocation object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AppDelegateReceiveTheLocation object:nil userInfo:nil];