2014-11-05 64 views
0

我的问题很简单,但我似乎无法弄清楚。连接到服务器,然后在mkmapview上绘制坐标

我想添加一个刷新按钮到我的mapview,将返回到服务器,并检索新的位置,如果有任何更新。我的下面的代码可以用viewdidload方法首次调用服务器,并可以将服务器上的所有位置绘制到地图上。我现在需要的是一个按钮,每次按下时都会进行相同的调用,我为所有的代码使用了一个类,所以请您最简单的解决方案,将很容易与代码合并将非常感激。

我也是很新的ios编程,所以任何关于如何整理我的代码的建议也将不胜感激。

这是我的视图控制器,它包含我所有的代码。

//ViewController.m 

#import "ViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.mapView.delegate = self; 

locationManager = [[CLLocationManager alloc] init]; 
[locationManager setDelegate:self]; 
[locationManager setDistanceFilter:kCLDistanceFilterNone]; 
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { 

    [self.mapView setShowsUserLocation:YES]; 

} else { 
    [locationManager requestWhenInUseAuthorization]; 
} 

NSURL *jsonFileUrl = [NSURL URLWithString:@"http://sample.name/service.php"]; 
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl]; 
[NSURLConnection connectionWithRequest:urlRequest delegate:self]; 
} 

#pragma mark NSURLConnectionDataProtocol Methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
_downloadedData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[_downloadedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

NSMutableArray *_locations = [[NSMutableArray alloc] init]; 

NSError *error; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error]; 

CLLocationCoordinate2D coordinate; 

for (int i = 0; i < jsonArray.count; i++) 
{ 
    NSDictionary *jsonElement = jsonArray[i]; 

    MKPointAnnotation* marker = [[MKPointAnnotation alloc] init]; 

    marker.title = jsonElement[@"Name"]; 
    marker.subtitle = jsonElement[@"Address"]; 
    coordinate.latitude = [jsonElement [@"Latitude"] doubleValue]; 
    coordinate.longitude = [jsonElement [@"Longitude"] doubleValue]; 

    marker.coordinate = coordinate; 
    [_locations addObject:marker]; 
} 

[self.mapView addAnnotations:_locations]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation 
{ 
static NSString *identifier; 
{ 
    if (annotation == mapView.userLocation) return nil; 

    MKAnnotationView *annotationView; 
    if (annotationView == nil) { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 
     annotationView.image = [UIImage imageNamed:@"blue_pin.png"]; 
    } else { 
     annotationView.annotation = annotation; 
    } 
    return annotationView; 
} 

return nil; 
} 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) { 
    [self.mapView setShowsUserLocation:YES]; 
} 
} 

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
CLLocationCoordinate2D myLocation = [userLocation coordinate]; 
MKCoordinateRegion zoomRegion = MKCoordinateRegionMakeWithDistance(myLocation, 10000, 10000); 
[self.mapView setRegion:zoomRegion animated:YES]; 
} 

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)InterfaceOrientation 
{ 
return (InterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

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

- (IBAction)Refresh:(id)sender { 
} 
@end 

回答

1

您可以通过评论找到我的解决方案。

当您刷新或获得响应时删除旧数据。

- (IBAction)action_goToManageDevice:(id)sender 
{ 
    [self reloadData]; 
} 

- (void)reloadData 
{ 
    // Remove old annotation 
    [self.mapView removeAnnotations:self.mapView.annotations]; 

    // reload your data 
    NSURL *jsonFileUrl = [NSURL URLWithString:@"http://sample.name/service.php"]; 
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl]; 
    [NSURLConnection connectionWithRequest:urlRequest delegate:self]; 
} 
+0

谢谢,你的回答解决了我的问题 – 2014-11-06 06:56:05