2

我有个问题。我用 google apis 搜索地点,让我在地图上创建一个注释图钉。数据存储在字典中。我尝试将这些位置与包含数据的标注一起传输到另一个 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 真的很陌生,所以每个提示对我都很有用!

问候柯蒂斯

4

1 回答 1

0

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

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

参考

于 2013-02-01T08:35:06.220 回答