出于某种原因,我似乎无法使用tap和longtap以及所有其他委托事件在我的模拟器中工作。我认为我正确安装了所有内容,因为地图显示在屏幕上,点击自定义按钮我可以在地图上放置标记,但它无法识别委托事件。我做错了吗?
mapviewcontroller.h
#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController <GMSMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UIView *mapContainer;
- (IBAction)addmarker:(id)sender;
@end
mapviewcontroller.m
#import "MapViewController.h"
@interface MapViewController ()
@end
@implementation MapViewController{
GMSMapView *mapView;
}
@synthesize mapContainer;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
mapView.settings.compassButton = YES;
//CLLocation *myLoc = mapView.myLocation;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapContainer.frame.size.width, self.mapContainer.frame.size.height) camera:camera];
[self.mapContainer addSubview:mapView];
}
- (void) mapView: (GMSMapView *) mapView didTapAtCoordinate: (CLLocationCoordinate2D) coordinate{
NSLog(@"%f, %f", coordinate.latitude, coordinate.longitude);
}
- (void) mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate{
NSLog(@"tapped");
}
- (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker{
NSLog(@"tapped info");
}
- (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker{
NSLog(@"dragged");
}
- (IBAction)addmarker:(id)sender {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"hello";
marker.map = mapView;
}
@end
唯一有效的是IBAction。我的位置和指南针按钮也不显示。为什么它不起作用的任何想法?我宣布代表们......
答案 0 :(得分:1)
您需要在对象初始化后设置所有属性。
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapContainer.frame.size.width, self.mapContainer.frame.size.height) camera:camera];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
mapView.settings.compassButton = YES;
[self.mapContainer addSubview:mapView];
}