0

对这里的 xcode 来说很新。我能够获取 flickr 数据,然后在单击较小的图像时显示较大的图像。

从 Flickr 我也得到了经度和纬度数据。

我希望能够显示带有每个图像的经纬度的地图,然后使用 mapview 将其显示到 xib 上。

如何将数据从 WebserviceEXVC.m 传递给 DVC.H 和 DVC.m?(我已经在xib上显示更大的图像。我想用地图替换更大的图像。我该如何在地图视图中显示长和纬度?

        WebServiceExampleVC.h





    //
    //  WebServiceExampleViewController.h
    //  WebServiceExample
    //
    //  Created by Computerlab on 7/22/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface WebServiceExampleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{

        NSMutableArray* photoTitles;
        NSMutableArray* photoSmallImageData;
        NSMutableArray* photoURLsLargeImage;
        //latitude":48.886392, "longitude"
        NSMutableArray* photolongitude;
        NSMutableArray* photolatitude;


        NSMutableArray* photoURLsLargeData;

        NSURLConnection* connection;
        NSMutableData* receivedData;

    }    

    @property (nonatomic, strong) IBOutlet UITableView* myTableView;

    -(void)searchFlickrPhotos:(NSString*)text;

    @end



        WebServiceExampleVC.m





     //
        //  WebServiceExampleViewController.m
        //  WebServiceExample
        //
        //  Created by Computerlab on 7/22/12.
        //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
        //

        #import "WebServiceExampleViewController.h"
        #import "DVC.h"


        //NSString *const FlickrAPIKey = @"ac35247e7b67528944f322cceb05704b";
        //My Key
        NSString *const FlickrAPIKey = @"00af42177056c5b71bda9bd49938c1df";

        @interface WebServiceExampleViewController ()

        @end

        @implementation WebServiceExampleViewController
        @synthesize myTableView;


        -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
            static NSString* CellIdentifier = @"Cell";
            int row = indexPath.row;

            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

            if(cell == nil){
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
                NSLog(@"%d - NEW CELL", row);
            }
            else {
                NSLog(@"%d - OLD CELL", row);
            }

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

            UILabel* x2 = cell.textLabel;
            CGRect r = x2.frame;
            r.size.height = 100;
            x2.frame = r;
            x2.text = [NSString stringWithFormat:@"%d", row];

            UILabel* detailText = cell.detailTextLabel;
           // detailText.text = [NSString stringWithFormat:@"%s", "title"];


            NSString* title = (NSString*) [photoTitles objectAtIndex:row];
            detailText.text = [NSString stringWithFormat:@"%@", title];
            NSString* lat = (NSString*) [photolatitude objectAtIndex:row];
            detailText.text = [NSString stringWithFormat:@"%@", lat];
            NSString* longi = (NSString*) [photolongitude objectAtIndex:row];
            detailText.text = [NSString stringWithFormat:@"Lat %@\r\n,Long %@\n\n,title %@\n\n", lat ,longi, title];
            //detailText.text =[NSString stringWithFormat:latitude];
            // detailText.text = [NSString stringWithFormat:@"%s", "Longitude"];
            UIImageView* imageView = cell.imageView;
            NSData* imageData = (NSData*) [photoSmallImageData objectAtIndex:row];
            imageView.image = [UIImage imageWithData:imageData];
            return cell;

        }


        - (void)connectionDidFinishLoading:(NSURLConnection *)connection{

            NSError* error;

            NSDictionary* results = [NSJSONSerialization JSONObjectWithData:receivedData
                                                                    options:kNilOptions
                                                                      error:&error];

            NSArray *photos = [[results objectForKey:@"photos"] objectForKey:@"photo"];

            for (NSDictionary *photo in photos)
            {
                NSString *title = [photo objectForKey:@"title"];
                [photoTitles addObject:(title.length > 0 ? title : @"Untitled")];
                NSLog(@"title: %@", title);

                NSString *latitude = [photo objectForKey:@"latitude"];
               [photolatitude addObject: [NSString stringWithFormat:@"%@", latitude]];
                //NSLog(@"photolatitude: %@", photolatitude);
                 NSLog(@"latitude: %@", latitude);

                NSString *longitude = [photo objectForKey:@"longitude"];
                [photolongitude addObject: [NSString stringWithFormat:@"%@", longitude]];
                //NSLog(@"photolongitude: %@",photolongitude);
                NSLog(@"longitude: %@",longitude);

                // Build the URL to where the image is stored (see the Flickr API)
                // In the format http://farmX.static.flickr.com/server/id/secret
                // Notice the "_s" which requests a "small" image 75 x 75 pixels
                NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_s.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]]; 
                NSLog(@"photoURLString: %@", photoURLString);
                [photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];
                photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
                [photoURLsLargeImage addObject:[NSURL URLWithString:photoURLString]]; 
                NSLog(@"photoURLsLareImage: %@\n\n", photoURLString); 


                [photoURLsLargeData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];

                  //NSLog(@"photoURLsLargeData: %@\n\n", photoURLsLargeData);






            }

            NSLog(@"Size of dictionary: %d", photoSmallImageData.count);
            [myTableView reloadData];
        }


        -(void)searchFlickrPhotos:(NSString *)text
        {
            // Build the string to call the Flickr API
          /*  NSString *urlString =
            [NSString stringWithFormat:
             @"http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&tags=%@&per_page=50&format=json&nojsoncallback=1", FlickrAPIKey, text];
            */

            // Build the string to call the Flickr API
            NSString *urlString =
            [NSString stringWithFormat:
             @"http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&tags=%@&per_page=5&format=json&nojsoncallback=1&has_geo=1&extras=geo", FlickrAPIKey, text];


            NSLog(@"REQ SENT: %@", urlString);
            NSURL *url = [NSURL URLWithString:urlString];

            // Setup and start async download
            NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
            connection = [[NSURLConnection alloc] initWithRequest:request 
                                                         delegate:self];

        }


        -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response{
            [receivedData setData:nil];
        }

        -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data{
            [receivedData appendData:data];
        }



        -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
            return photoSmallImageData.count;
        }


        -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
            return 150.0f;
        }



        - (void)viewDidLoad
        {
            [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
            photoTitles = [[NSMutableArray alloc] init];
            photoSmallImageData = [[NSMutableArray alloc] init];
            photoURLsLargeImage = [[NSMutableArray alloc] init];
            photoURLsLargeData = [[NSMutableArray alloc] init];
            photolongitude = [[NSMutableArray alloc] init];
            photolatitude = [[NSMutableArray alloc] init];

            receivedData = [[NSMutableData alloc] init];
            [self searchFlickrPhotos:@"cars"];

        }

        - (void)viewDidUnload
        {
            [super viewDidUnload];
            // Release any retained subviews of the main view.
        }

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        }


        -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


        NSData* imageData = (NSData*) [photoURLsLargeData objectAtIndex:indexPath.row];
            [[NSUserDefaults standardUserDefaults] setObject:imageData
                                                      forKey:@"largeImage"];
            [[NSUserDefaults standardUserDefaults] synchronize];

            //[[NSUserDefaults standardUserDefaults] setObject:<#(id)#> forKey:<#(NSString *)#>]

            DVC* detailView = [[DVC alloc] initWithNibName:@"DVC"
                                                    bundle:nil];

            [self.navigationController pushViewController:detailView
                                                 animated:YES];

        }




        @end




        DVC.h





 /*****
    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>

    @interface mapiphoneViewController : UIViewController <MKMapViewDelegate>


    @property (strong, nonatomic) IBOutlet MKMapView* map;



    @end
    ****/

        //
        //  DVC.h
        //  WebServiceExample
        //
        //  Created by Computerlab on 7/29/12.
        //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
        //

        #import <UIKit/UIKit.h>

        @interface DVC : UIViewController

        @property (strong, nonatomic) IBOutlet UIImageView* myImageView;

        @end


        DVC.m



  //
    //  DVC.m
    //  WebServiceExample
    //
    //  Created by Computerlab on 7/29/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //

    #import "DVC.h"

    @interface DVC ()

    @end

    @implementation DVC
    @synthesize myImageView;

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

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        NSData* myData = [[NSUserDefaults standardUserDefaults] dataForKey:@"largeImage"];
        myImageView.image = [UIImage imageWithData:myData];


    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    @end




    ///Map stuff

/*****************************************************    
    @synthesize map;

    -(MKOverlayView*) mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay  {

        MKCircleView* circleView =[[MKCircleView alloc] initWithOverlay:overlay];
        circleView.strokeColor = [UIColor blueColor];
        circleView.fillColor = [UIColor redColor];
        return circleView;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        float latitude = 40.0;
        float longitude  = -75.0;

        CLLocationCoordinate2D x;
        x.latitude = latitude;
        x.longitude = longitude;

        MKCoordinateSpan z;
        z.latitudeDelta = 0.15;
        z.longitudeDelta = 0.15;

        MKCoordinateRegion y;
        y.center = x;
        y.span = z;

        map.region = y;
        [map addOverlay:[MKCircle circleWithCenterCoordinate:x
                                                      radius:1000]];
    }


******/
4

1 回答 1

0

您可以使用“centerCoordinate”属性设置 MKMapView 中心点https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/centerCoordinate

于 2012-08-11T21:48:34.887 回答