从变量将值传递给 CLLocationCoordinate2D 时,我遇到了这个奇怪的问题。但是,如果我使用硬编码的相同值。然后它完美地工作。
我正在从此页面获取值。 http://localsearch.azurewebsites.net/api/Locations
将其作为字典存储在 NSMutable 数组中。所有值都可以存储并可以毫无问题地检索。我将所有内容都作为字符串获取,然后将它们类型转换为任何必要的内容。在这种情况下,双打或浮点数将是经纬度(我尝试过两者都以相同的结果结束)。
我在它们出现在 CL CLLocationCoordinate2D 上之前打印这些值。他们很好。与硬编码的值相同。我已经截取了一些屏幕截图,希望可能有人遇到过这个问题。
此屏幕截图用于显示我的 coord var 从上面的硬编码值中获得的值。
现在,此屏幕截图显示了我的 coord2 var 从存储与以前通过硬编码的相同值的变量获取的值。在底部,您可以看到 NSLog 打印相同的值。但由于某种原因,它们确实得到了不同的值。
谢谢您的帮助。PS:如果你们需要我添加我所做的代码片段,请告诉我。不确定是否需要它,因为这是值不匹配的特定位置。
这是我的表格视图代码
-(void)viewWillAppear:(BOOL)animated{
//Fetching data from API URL
NSURL *locationURL = [NSURL URLWithString:JSON_URL];
NSData *jsonData = [NSData dataWithContentsOfURL:locationURL];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
for (NSDictionary *locationsDictionary in dataDictionary){
LocationObjects *currentLocation = [[LocationObjects alloc] initWithId:[locationsDictionary objectForKey:@"ID"]
Name:[locationsDictionary objectForKey:@"Name"]
Latitude:[locationsDictionary objectForKey:@"Latitude"]
Longitude:[locationsDictionary objectForKey:@"Longitude"]
Address:[locationsDictionary objectForKey:@"Address"]
ArrivalTime:[locationsDictionary objectForKey:@"ArrivalTime"]];
[self.objectHolderArray addObject:currentLocation];
}
//[objectHolderSortedArray arrayByAddingObjectsFromArray:[self sortArray]];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
UIBarButtonItem *btn=[[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain
target:self
action:@selector(dismissView)];
self.navigationItem.leftBarButtonItem=btn;
}
-(NSMutableArray *)objectHolderArray{
if(!objectHolderArray) objectHolderArray = [[NSMutableArray alloc]init];
return objectHolderArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//NSLog(@"returns the count in the array: %lu", (unsigned long)[self.objectHolderArray count]);
return [self.objectHolderArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
LocationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[LocationTableViewCell alloc] initWithFrame:CGRectZero];
}
LocationObjects *currentLocation = [self.objectHolderArray objectAtIndex:indexPath.row];
cell.lblId.text = [NSString stringWithFormat:@"ID: %@",currentLocation.Id];
cell.lblcurrentName.text = [NSString stringWithFormat:@"Name: %@",currentLocation.Name];
cell.lblgivenAddress.text = [NSString stringWithFormat:@"Address: %@",currentLocation.Address];
return cell;
}
#pragma mark - Table View Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
LocationObjects *currentLocation = [self.objectHolderArray objectAtIndex:indexPath.row];
//NSLog(@"indexPath.row: %lu", indexPath.row);
if(!LocationMapVC) LocationMapVC = [[LocationMapViewController alloc] init];
LocationMapVC.name = [NSString stringWithFormat:@"Name: %@",currentLocation.Name];
//NSLog(@"Name: %@", [NSString stringWithFormat:@"Name: %@",currentLocation.Name]);
LocationMapVC.address = [NSString stringWithFormat:@"Address: %@",currentLocation.Address];
LocationMapVC.latitude = [NSString stringWithFormat:@"Latitude: %@",currentLocation.Latitude];
NSLog(@"Latitude: %@", [NSString stringWithFormat:@"Latitude: %@",currentLocation.Latitude]);
LocationMapVC.longitude = [NSString stringWithFormat:@"Longitude: %@",currentLocation.Longitude];
NSLog(@"Longitude: %@", [NSString stringWithFormat:@"Longitude: %@",currentLocation.Longitude]);
//http://stackoverflow.com/questions/33758859/present-view-controller-not-working-with-ios-9
[self presentViewController:LocationMapVC animated:YES completion:nil];
LocationMapVC = nil;
}
这是我的地图视图代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//late instantiation
if (!mapView) {
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
}
//dismiss modal button
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchDown];
[done setTitle:@"Done" forState:UIControlStateNormal];
done.frame = CGRectMake(((self.view.frame.size.width)/2)+10, (self.view.frame.size.height)-60, 160, 40);
done.backgroundColor = [UIColor blackColor];
//direction modal button
UIButton *directions = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[directions addTarget:self action:@selector(showRouteVC) forControlEvents:UIControlEventTouchDown];
[directions setTitle:@"Route" forState:UIControlStateNormal];
directions.frame = CGRectMake(((self.view.frame.size.width)/2)-170, (self.view.frame.size.height)-60, 160, 40);
directions.backgroundColor = [UIColor blackColor];
//Location Manager
locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
//[locationManager requestLocation];
//[locationManager startUpdatingLocation];
self.mapView.delegate = self;
mapView.mapType = MKMapTypeStandard;
//mapView.showsUserLocation = YES;
self.mapView.zoomEnabled = YES;
//MAPING VALUES
CLLocationCoordinate2D coord = {.latitude = 41.883976, .longitude = -87.639346};
NSLog(@"In location map the Latitude: %@ and Longitutude: %@", latitude, longitude);
CLLocationCoordinate2D coord2 = {.latitude = [latitude doubleValue],.longitude = [longitude doubleValue]};
MKCoordinateSpan span = {.latitudeDelta = 0.03, .longitudeDelta = 0.03};
MKCoordinateRegion region = {coord, span};
//implementing annotations base of the info passed.
annotation = [[Annotation alloc] initWithAnnotation:coord title:name subtitle:address];
[self.mapView addAnnotation:annotation];
[self.mapView setRegion:region animated:YES];
//adding subviews.
[mapView setRegion:region];
[self.view addSubview:mapView];
[self.view addSubview:done];
[self.view addSubview:directions];
}