2013-02-26 70 views
1

早上好StackOverflow,得到我的自定義註釋呼叫在我的'europeMapView'上工作後,我現在需要將用戶發送到webView,現在我想讓webView從JSON文件中加載一個URL,具體取決於哪個標註是點擊。我相信我應該使用NSJSONSerialization,雖然我不知道如何實現這個?自定義註釋以可能的JSON發送到webView?

所以在'europeMapView'當'羅馬'呼出按鈕被點擊時,它將使用戶'europeWebView'並加載字符串www.ThingsAboutRome.com。

我想我可以只爲每個註釋擁有一個webView,但這似乎非常低效。 (加上我無法即時更改網址)。

我也在使用Storyboard。

#import "UKFlightsEuropeMap.h" 
#import "Annotation.h" 



@interface UKFlightsEuropeMap() 

@end 

@implementation UKFlightsEuropeMap 
@synthesize europeMapView; 

//Define the Long and Lat of different European cities. 


#define EUROPE_LATITUDE 47.3690; 
#define EUROPE_LONGITUDE 8.5380; 

#define LONDON_LATITUDE 51.5171; 
#define LONDON_LONGITUDE 0.1062; 

#define PARIS_LATITUDE 48.8742; 
#define PARIS_LONGITUDE 2.3470; 

#define BRUSSELS_LATITUDE 50.75; 
#define BRUSSELS_LONGITUDE 4.53333; 

#define BERLIN_LATITUDE 52.5; 
#define BERLIN_LONGITUDE 13.35; 

#define MUNICH_LATITUDE 48.1333; 
#define MUNICH_LONGITUDE 11.5667; 

#define ZURICH_LATITUDE 47.3690; 
#define ZURICH_LONGITUDE 8.5380; 

#define MILAN_LATITUDE 45.4640; 
#define MILAN_LONGITUDE 9.1916; 

#define MADRID_LATITUDE 40.4000; 
#define MADRID_LONGITUDE -3.6833; 

#define ROME_LATITUDE 41.9000; 
#define ROME_LONGITUDE 12.5000; 

#define LISBON_LATITUDE 38.7000; 
#define LISBON_LONGITUDE 9.1833; 

#define VIENNA_LATITUDE 48.2088; 
#define VIENNA_LONGITUDE 16.3726; 

#define THE_SPAN 0.01f; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

[europeMapView setDelegate:self]; 

// Do any additional setup after loading the view. 

//Create my region 

MKCoordinateRegion myRegion; 

//Set region centre 
CLLocationCoordinate2D center; 
center.latitude = EUROPE_LATITUDE; 
center.longitude = EUROPE_LONGITUDE; 

//Set span (zoom level) 
MKCoordinateSpan span; 
span.latitudeDelta = THE_SPAN; 
span.longitudeDelta = THE_SPAN; 

myRegion.center = center; 
myRegion.span = span; 

//Set our region to the europeMapView 

[europeMapView setRegion:myRegion animated:YES]; 


//Annotation 

NSMutableArray * locations = [[NSMutableArray alloc] init]; 
CLLocationCoordinate2D location; 
Annotation * myAnn; 

//London Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = LONDON_LATITUDE; 
location.longitude = LONDON_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"London"; 
myAnn.subtitle = @"London City"; 


[locations addObject:myAnn]; 

//Paris Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = PARIS_LATITUDE; 
location.longitude = PARIS_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Paris"; 
myAnn.subtitle = @"City of Love"; 
[locations addObject:myAnn]; 

//Brussels Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = BRUSSELS_LATITUDE; 
location.longitude = BRUSSELS_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Brussels"; 
myAnn.subtitle = @"Brussels"; 
[locations addObject:myAnn]; 

//Berlin Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = BERLIN_LATITUDE; 
location.longitude = BERLIN_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Berlin"; 
myAnn.subtitle = @"Berlin"; 
[locations addObject:myAnn]; 

//Munich Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = MUNICH_LATITUDE; 
location.longitude = MUNICH_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Munich"; 
myAnn.subtitle = @"Munich"; 
[locations addObject:myAnn]; 

//Zurich Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = ZURICH_LATITUDE; 
location.longitude = ZURICH_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Zurich"; 
myAnn.subtitle = @"Zurich"; 
[locations addObject:myAnn]; 

//Milan Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = MILAN_LATITUDE; 
location.longitude = MILAN_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Milan"; 
myAnn.subtitle = @"Milan"; 
[locations addObject:myAnn]; 

//Madrid Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = MADRID_LATITUDE; 
location.longitude = MADRID_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Madrid"; 
myAnn.subtitle = @"Madrid"; 
[locations addObject:myAnn]; 

//Rome Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = ROME_LATITUDE; 
location.longitude = ROME_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Rome"; 
myAnn.subtitle = @"Rome"; 
[locations addObject:myAnn]; 

//Lisbon Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = LISBON_LATITUDE; 
location.longitude = LISBON_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Lisbon"; 
myAnn.subtitle = @"Lisbon"; 
[locations addObject:myAnn]; 

//Vienna Annotation 

myAnn = [[Annotation alloc] init]; 
location.latitude = VIENNA_LATITUDE; 
location.longitude = VIENNA_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"Vienna"; 
myAnn.subtitle = @"Vienna"; 
[locations addObject:myAnn]; 

//Add the NSMutable Array to the europeMapView. 


[self.europeMapView addAnnotations:locations]; 




} 

//Customize the annotation callouts. 

-(MKAnnotationView *) mapView:(MKMapView *)europeMapView viewForAnnotation:  (id<MKAnnotation>)annotation { MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc]  initWithAnnotation:annotation reuseIdentifier:@"current"]; 
MyPin.pinColor = MKPinAnnotationColorPurple; 

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
if ([[annotation title] isEqualToString:@"London"]){ 
    [advertButton addTarget:self action:@selector(LondonClicked:)  forControlEvents:UIControlEventTouchUpInside];   
} 

if ([[annotation title] isEqualToString:@"Paris"]){ 
    [advertButton addTarget:self action:@selector(ParisClicked:)  forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Brussels"]){ 
    [advertButton addTarget:self action:@selector(BrusselsClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Berlin"]){ 
    [advertButton addTarget:self action:@selector(BerlinClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Munich"]){ 
    [advertButton addTarget:self action:@selector(MunichClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Zurich"]){ 
    [advertButton addTarget:self action:@selector(ZurichClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Milan"]){ 
    [advertButton addTarget:self action:@selector(MilanClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Madrid"]){ 
    [advertButton addTarget:self action:@selector(MadridClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Rome"]){ 
    [advertButton addTarget:self action:@selector(RomeClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Lisbon"]){ 
    [advertButton addTarget:self action:@selector(LisbonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 

if ([[annotation title] isEqualToString:@"Vienna"]){ 
    [advertButton addTarget:self action:@selector(ViennaClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 


MyPin.rightCalloutAccessoryView = advertButton; 
MyPin.draggable = NO; 
MyPin.highlighted = YES; 
MyPin.animatesDrop = TRUE; 
MyPin.canShowCallout = YES; 


return MyPin; 

} 

//Show that the detailDisclosure Button has been pressed. 

-(void)LondonClicked:(id)sender { 
NSLog(@"London Clicked"); 
} 
-(void)ParisClicked:(id)sender { 
NSLog(@"Paris Clicked"); 
} 
-(void)BrusselsClicked:(id)sender { 
NSLog(@"Brussels Clicked"); 
} 
-(void)BerlinClicked:(id)sender { 
NSLog(@"Berlin Clicked"); 
} 
-(void)MunichClicked:(id)sender { 
NSLog(@"Munich Clicked"); 
} 
-(void)ZurichClicked:(id)sender { 
NSLog(@"Zurich Clicked"); 
} 
-(void)MilanClicked:(id)sender { 
NSLog(@"Milan Clicked"); 
} 
-(void)MadridClicked:(id)sender { 
NSLog(@"Madrid Clicked"); 
} 
-(void)RomeClicked:(id)sender { 
NSLog(@"Rome Clicked"); 
} 
-(void)LisbonClicked:(id)sender { 
NSLog(@"Lisbon Clicked"); 
} 
-(void)ViennaClicked:(id)sender { 
NSLog(@"Vienna Clicked"); 
} 


- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 


@end 

編輯:JSON文件的示例。

{ 
"ID": "London", 
"Link": "http://www.London.co.uk", 
"ID": "Paris", 
"Link": "http://www.Paris.fr" 
} 

非常感謝您的時間。

Nick。

+0

我不明白你的情況completey:你是否將數據存儲在json(文件)中,就像一個位置是一個對象,在那裏你可以找到要點擊的鏈接?您可以使用loadRequest:方法來更改webview的頁面。 – 2013-02-26 11:31:53

+0

是的,所以我會將URL存儲到JSON文件中的各個站點(可通過Web地址訪問),我只是不知道如何實現一個方法來告訴webView加載哪個Web地址取決於哪個註釋是點擊。 – 2013-02-26 12:51:26

+0

你的城市是否有硬編碼?我想他們是,否則你已經知道如何從JSON加載它們,對吧?如果他們是硬編碼的,您可能需要爲他們設置一個密鑰,以便您可以將硬編碼城市鏈接到JSON中的一個城市。你可以添加一個你的JSON的例子嗎? – 2013-02-26 12:59:33

回答

0

首先我建議改變你的數據格式是這樣的:

{ 
    "london":{ 
     "link":"http://www.London.co.uk", 
     "moredata":"..." 
    }, 
    "paris":{ 
     "link":"http://www.Paris.fr", 
     "moredata":"..." 
    } 
} 

與它相關聯的屬性這將代表一個城市。在您的版本中,查找特定城市的正確鏈接是不可能的。

然後你就可以解析和訪問不同的部分如下:

NSString *json = @"{\"london\":{\"link\":\"http://www.London.co.uk\",\"moredata\":\"...\"},\"paris\":{\"link\":\"http://www.Paris.fr\",\"moredata\":\"...\"}}"; 

NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; 

NSError *error; 

NSDictionary *cities = [NSJSONSerialization JSONObjectWithData:data options:nil error:&error]; 

NSLog(@"link to london: %@", cities[@"london"][@"link"]); 

>> link to london: http://www.London.co.uk 

這個你必須與提取的鏈接餵你的WebView後。

+0

謝謝你的簡潔的答案!我會給它一個。 – 2013-02-26 15:51:02

+0

附註(以防萬一):請不要遷移垃圾郵件(重新:*解鎖的iPhone可以使用預付費SIM卡與羅傑斯* *)。另外,如果您不知道目標網站的常見問題,請不要投票遷移。電話和電話服務問題與[su]無關。謝謝。 – Will 2013-03-27 12:07:26