gpt4 book ai didi

objective-c - 用 canShowCallout 切换 viewController 的注解

转载 作者:行者123 更新时间:2023-12-01 16:53:41 26 4
gpt4 key购买 nike

我有个问题。我使用 google apis 搜索地点并让我在 map 上创建一个注释图钉。数据存储在字典中。我尝试将这些位置与包含数据的标注一起传输到另一个 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 所说,您可以实现附件 View 委托(delegate),但您必须记住放置 UIControl 否则不会调用此方法 .否则,该 View 需要处理自己的触摸事件。
从文档中:

If the view you specify is also a descendant of the UIControl class, you can use the map view’s delegate to receive notifications when your control is tapped. If it does not descend from UIControl, your view is responsible for handling any touch events within its bounds.



reference

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com