我有问题。我用google apis搜索地方,让我在地图上创建一个注释引脚。数据存储在dict中。我尝试使用包含数据的标注转移到另一个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的新手,所以每个提示对我都有用!
关心柯蒂斯
答案 0 :(得分:0)
正如@Anna Karenina所说,你可以实现配件视图委托,但你必须记得放置一个UIControl 或者这个方法不会被称为。否则该视图需要处理自己的触摸事件。
来自文件:
如果您指定的视图也是UIControl类的后代, 您可以使用地图视图的委托来接收通知 控制被轻敲。如果它不是从UIControl下降,那么你的观点是 负责处理其范围内的任何触摸事件。