JSON
Web サービスからのデータを mapkit マップに入力しています。データの各行には一連の座標があり、マップに追加されます。各行には URL もあります。私のコードの問題は、各マップのアノテーション コールアウト アクセサリ ボタンで同じ URL (常に配列のデータの最後の行) を使用していることです。リンクは、辞書からの最後のリンクです。4 行目の下のNSLOG
出力は、各コールアウトに使用されるリンクです。もちろん、各コールアウトには独自の URL が必要です。はdealAnnotation.title = [currentDeal objectForKey:@"vendor"];
、各マップ オブジェクトの正しいベンダー名を表示しています。ディクショナリの最後の URL を常に表示するのは URL だけです。
ログは次のとおりです。
2013-02-27 11:17:35.077 link populated from map is http://www.http://www.****link1
2013-02-27 11:17:35.078 link populated from map is http://www.http://www.****link5
2013-02-27 11:17:35.079 link populated from map is http://www.http://www.****link3
2013-02-27 11:17:35.079 link populated from map is http://www.http://www.****link4
これが私のコードです:
右吹き出しボタン メソッドを使用した MKAnnotationpushToSafari
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if ([annotation isMemberOfClass:[MKUserLocation class]]) return nil;
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
// annView.pinColor = MKPinAnnotationColorGreen;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(pushToSafari)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
// annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
//add custom yd pin
annView.image = [UIImage imageNamed:@"icon-pin-2.png"];
return annView;
}
マップにデータを入力するコード。これは、link = [currentDeal objectForKey:@"link"];
すべてのコールアウト ボタンの JSON からの最後の URL を設定している行です。
#pragma mark - Populate Map
- (void)populateMap:(MKMapView*)map withDeals:(NSArray*) deals
{
NSLog(@"DEALS" );
for (NSDictionary *currentDeal in deals) {
CLLocationCoordinate2D ctrpoint;
ctrpoint.latitude = [[[currentDeal objectForKey:@"coords"] objectForKey:@"lat"] doubleValue];
ctrpoint.longitude =[[[currentDeal objectForKey:@"coords"] objectForKey:@"lng"] doubleValue];
MKPointAnnotation *dealAnnotation = [[MKPointAnnotation alloc] init];
dealAnnotation.coordinate = ctrpoint;
dealAnnotation.title = [currentDeal objectForKey:@"vendor"];
link = [currentDeal objectForKey:@"link"];
NSLog(@"link populated from map is %@",link);
NSDictionary *currDict = @{
@"EUR": @"€",
@"GBP": @"₤",
@"USD": @"$",
@"BRL": @"R$"
};
NSString *currName = [currentDeal objectForKey:@"currency"];
NSString *currency = [currDict objectForKey:currName];
dealAnnotation.subtitle = [NSString stringWithFormat:@"%@%i",currency,[[currentDeal objectForKey:@"price"] integerValue ]];
NSLog(@"current deal currency sym is %@",[currentDeal objectForKey:@"id"]);
[map setDelegate:self];
[map addAnnotation:dealAnnotation];
}
}
JSON コードを使用した viewDidAppear: メソッド:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"MAP VIEW APPEARED");
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSLog(@"%f %f",coordinate.latitude,coordinate.longitude );
if ( (double ) coordinate.latitude == 0 && (double ) coordinate.longitude == 0 ){
CustomAlertView *alert = [[CustomAlertView alloc]initWithTitle:@"No GPS Connection" message:@"GPS data is currently unavailable. Please ensure that Location Services are turned on in Settings." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
return;
}
CLLocationDegrees currentLongitude=coordinate.longitude;
CLLocationDegrees currentLatitude=coordinate.latitude;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(currentLatitude, currentLongitude);
[mapView setCenterCoordinate:center];
NSString *query = [NSString stringWithFormat:@"http://www.****.com/coords=45.4640,9.1916&country=%@&maxdistance=3000&api.ofilter=track:iphone",APP_ID,lang];
NSString *locationJsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
error:nil];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:locationJsonString error:nil];
NSString *currentCity = [[[results objectForKey:@"results"] objectAtIndex:0] objectForKey:@"key"];
NSLog(@"Current city is : %@",currentCity);
NSString *dealSearch = [NSString stringWithFormat:@"http://****coords=45.4640,9.1916&maxdistance=20&api.ofilter=track:iphone",APP_ID,currentCity];
NSString *dealsInCurrentLocationJsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:dealSearch] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding error:nil];
// SBJSON *parser2 = [[SBJSON alloc] init];
NSDictionary *dealResults = [parser objectWithString:dealsInCurrentLocationJsonString error: nil];
NSArray *listOfDeals = [dealResults objectForKey:@"results"];
[self populateMap:mapView withDeals:listOfDeals];
NSLog(@"dLongitude : %f", currentLongitude);
NSLog(@"dLatitude : %f", currentLatitude);
}
右吹き出しボタン タッチのメソッド:
-(IBAction)pushToSafari {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]];
NSLog(@"link touched from offers around me is %@",link);
}
助けてくれてありがとう