非常非常新的iOS編程,所以我感謝任何幫助。 我真正想要做的就是當按下按鈕時,將當前座標的變量傳遞給不同的視圖。我無法解決如何使用我目前的工作做到這一點 - 我陷入了一片混亂。我的項目基本上是由來自各種來源的大量代碼組成的。我會與你分享我認爲相關的部分,希望有人能夠至少指出我正確的方向!傳遞位置到不同的視圖與segue
這是從我MainViewController.m:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (currentPosition == nil) {
MKMapPoint point = MKMapPointForCoordinate(newLocation.coordinate);
double pointsPerMeter = MKMapPointsPerMeterAtLatitude(newLocation.coordinate.latitude);
double visibleDistance = pointsPerMeter * 500.0;
MKMapRect rect = MKMapRectMake(
point.x - visibleDistance, point.y - visibleDistance,
visibleDistance * 2, visibleDistance * 2);
[self.mapView setVisibleMapRect:rect animated:YES];
NSURL *url = [NSURL URLWithString:@"https://url.url/json.json"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
if (error != nil)
{
}
CLLocationCoordinate2D location;
NSMutableArray *newAnnotations = [NSMutableArray array];
MKPointAnnotation *newAnnotation;
for (NSDictionary *dictionary in array)
{
location.latitude = [dictionary[@"lat"] doubleValue];
location.longitude = [dictionary[@"lon"] doubleValue];
newAnnotation = [[MKPointAnnotation alloc] init];
newAnnotation.coordinate = location;
HUWMapAnnotation *annotation = [[HUWMapAnnotation alloc] initWithCoordinate:newAnnotation.coordinate];
annotation.messagetitle = dictionary[@"name"];
annotation.email = dictionary[@"message"];
annotation.username = dictionary[@"user"];
[self.mapView addAnnotation:annotation];
[newAnnotations addObject:newAnnotation];
}
}
currentPosition = newLocation;
}
- (void)viewDidLoad { [super viewDidLoad];
// Check if the user has enabled location services.
if ([CLLocationManager locationServicesEnabled]) {
// Create a location manager.
locationManager = [[CLLocationManager alloc] init];
// Set ourselves as it's delegate so that we get notified of position updates.
locationManager.delegate = self;
// Set the desired accuracy.
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Start tracking.
[locationManager startUpdatingLocation];
}
}
就像我說的,我已經大幅削減下來到我認爲相關的位。讓我知道如果我錯過了一些必要的東西。我已經準備好了一個按鈕,我的故事板上有一個按鈕,還有一個帶有標識符的segue。
我認爲我應該使用prepareForSegue
- 但我的問題是,我根本不知道如何讓我的座標變成這種情況。
我希望有人可以幫我(以及我對大量的複製粘貼代碼道歉!)
備註:與每米計算MKMapPoints相比,使用[MKCoordinateReg ionMakeWithDistance函數](https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/doc/uid/TP40008209-CH2-SW5)並執行setRegion而不是setVisibleRect。 – Anna
我會考慮實現這一點,謝謝你指出。 –