iOS 编程非常非常新,所以我很感激这方面的任何帮助。我真正想做的就是在按下按钮时将当前坐标的变量传递给不同的视图。我无法用我目前的工作弄清楚如何做到这一点——我完全陷入了混乱。我的项目本质上是由来自各种来源的大量代码组成的。我将与您分享我认为相关的部分,希望有人至少可以为我指明正确的方向!
这是来自我的 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
- 但我的问题是我根本不知道如何让我的坐标进入那种情况。
我希望有人能够帮助我(对于大量复制粘贴的代码,我深表歉意!)