2014-11-05 60 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