変数から CLLocationCoordinate2D に値を渡すときに、この奇妙な問題が発生します。しかし、ハードコードされた同じ値を使用する場合。その後、それは完全に機能します。
このページから値を取得しています。 http://localsearch.azurewebsites.net/api/Locations
NSMutable 配列に Dicts として保存します。すべての値が保存され、問題なく取得されます。すべてを文字列としてフェッチしてから、それらを必要なものに型キャストしています。この場合、倍長または浮動小数点数に対して緯度と経度になります(両方を試してみましたが、結果は同じでした)。
CLCLLocationCoordinate2Dに配置する前に値を出力します。そして彼らは元気です。ハードコーディングされたものと同じ値。スクリーンショットをいくつか撮りましたが、誰かがこの問題に遭遇したことを願っています。
このスクリーンショットは、coord var が上記のハードコードされた値から取得した値を表示するためのものです。
さて、このスクリーンショットは、coord2 変数が、以前にハードコードされたパスと同じ値を格納する変数から取得した値を示しています。下部に、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];
}