我正在使用來自JSON
Web服務的數據填充mapkit地圖。每行數據都有一組座標,並添加到地圖中。每一行也有一個URL。我的代碼的問題在於,每個地圖的註釋標註附件按鈕使用相同的URL(始終是數組中的最後一行數據)。鏈接是詞典的最後一個鏈接。在第4行下面的NSLOG
輸出中是用於每個標註的鏈接。當然,每個標註都應該有自己的URL。 dealAnnotation.title = [currentDeal objectForKey:@"vendor"];
正在爲每個地圖對象顯示正確的供應商名稱。這只是始終顯示字典中最後一個網址的網址。使用JSON數據填充地圖
這裏的日誌:
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
這裏是我的代碼:
的MKAnnotation與正確標註按鈕方法pushToSafari
-(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最後瀏覽的網址爲每個標註按鈕
#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];
}
}
的viewDidAppear:方法與JSON代碼:
- (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);
}
感謝您的幫助
你爲什麼稱它爲JSON?你正在處理數組和字典。數據起源於JSON的事實僅僅與此無關。 – 2013-02-27 12:04:52
@HotLicks謝謝!我做了更改 – hanumanDev 2013-02-27 12:07:49
查看問題時,不要帶入不相關的細節,這一點很重要。很高興知道數據是從JSON中派生出來的,因爲它告訴我們字典/數組樹會「很好地形成」,但是一旦你解析了JSON,就沒有任何關於結果數據的特定JSON,並且將其視爲JSON可能會讓你感到困惑。 – 2013-02-27 13:11:17