我有个问题。我使用Google API搜索到地方,然后在地图上创建注释图钉。数据存储在字典中。我尝试将这些位置与包含数据的标注一起传输到另一个ViewController。

这是我的代码的一部分:
.h文件:

#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapPoint.h"

#define kGOOGLE_API_KEY @"MyGoogleAPIsKey"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)



@interface FirstViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    CLLocationCoordinate2D currentCentre;
    BOOL firstLaunch;
    int currenDist;
}

- (IBAction)myButton:(UIBarButtonItem *)sender;
- (IBAction)myPosition:(UIBarButtonItem *)sender;

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

@end

.m文件:
@implementation FirstViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Make this controller the delegate for the map view.
    self.mapView.delegate = self;

    // Ensure that you can view your own location in the map view.
    [self.mapView setShowsUserLocation:YES];

    //Instantiate a location object.
    locationManager = [[CLLocationManager alloc] init];

    //Make this controller the delegate for the location manager.
    [locationManager setDelegate:self];

    //Set some parameters for the location object.
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    firstLaunch=YES;

    [_mapView setCenterCoordinate:_mapView.userLocation.coordinate];

}
-(void)plotPositions:(NSArray *)data {
    // 1 - Remove any existing custom annotations but not the user location blue dot.
    for (id<MKAnnotation> annotation in _mapView.annotations)
    {
        if ([annotation isKindOfClass:[MapPoint class]])
        {
            [_mapView removeAnnotation:annotation];
        }
    }
    // 2 - Loop through the array of places returned from the Google API.
    for (int i=0; i<[data count]; i++)
    {
        //Retrieve the NSDictionary object in each index of the array.
        NSDictionary* place = [data objectAtIndex:i];
        // 3 - There is a specific NSDictionary object that gives us the location info.
        NSDictionary *geo = [place objectForKey:@"geometry"];
        // Get the lat and long for the location.
        NSDictionary *loc = [geo objectForKey:@"location"];
        // 4 - Get your name and address info for adding to a pin.
        NSString *name = [place objectForKey:@"name"];
        NSString *vicinity = [place objectForKey:@"vicinity"];
        // 4.5 - Get id for detailed view.
        NSString *myId = [place objectForKey:@"id"];
        // Create a special variable to hold this coordinate info.
        CLLocationCoordinate2D placeCoord;
        // Set the lat and long.
        placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
        placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];

        // 5 - Create a new annotation.
        MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity coordinate:placeCoord];
        [_mapView addAnnotation:placeObject];

        NSLog(@"%@", myId);
    }

}

-(void)button:(id)sender
{
    //here is my main problem ...
}

如果我做错了,还是请告诉我。
感谢您的回答!

好吧,忘了说我对Obj-C真的很陌生,所以每个技巧对我都有用!

问候柯蒂斯

最佳答案

正如@Anna Karenina所说,您可以实现附件视图委托BUT,但您必须记住要放置UIControl ,否则此方法将不称为。否则,该视图需要处理自己的触摸事件。
从文档中:

如果您指定的视图也是UIControl类的后代,
您可以使用地图视图的委托在您的
点击控制。如果不是UIControl派生的,则您的视图为
负责处理其范围内的任何触摸事件。

reference

关于objective-c - 带有canShowCallout开关viewController的注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13684157/

10-12 16:17