Search code examples
iosobjective-cxcodegoogle-mapsgoogle-polyline

Tapping polyline does nothing - Google Maps IOS SDK


Have read similar posts and tried their solutions but nothing's working for me. Have super simple Google Maps SDK 1.9.x example running on IOS Simulator through XCode 6.x, when I tap (OK, really clicking in the simulator, does that make a difference?) nothing gets logged as the code would lead me to believe it would, so it appears didTapOverlay never gets called.

#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController ()

@end

@implementation ViewController {
    GMSMapView *mapView_;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:43.5157
                                                            longitude:-114.2933
                                                                 zoom:14];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.myLocationEnabled = YES;
    mapView_.settings.compassButton = YES;
    mapView_.settings.myLocationButton = YES;


    GMSMutablePath *path2 = [GMSMutablePath path];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50618099,-114.34367)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50624301,-114.343572)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50635399,-114.343333)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50644602,-114.34312)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50655801,-114.342884)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50667996,-114.342642)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.506801,-114.342368)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50693896,-114.342054)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50707299,-114.341756)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50719796,-114.341476)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50731196,-114.341215)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.507419,-114.340978)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.50751396,-114.340759)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.507607,-114.340546)];
    [path2 addCoordinate:CLLocationCoordinate2DMake(43.507736,-114.340248)];

    GMSPolyline *rectangle2 = [GMSPolyline polylineWithPath:path2];
    rectangle2.spans = @[[GMSStyleSpan spanWithColor:[UIColor redColor]]];
    rectangle2.tappable = true;
    rectangle2.strokeWidth = 10.f;

    rectangle2.map = mapView_;

    NSLog(@"The code runs through here!");

    self.view = mapView_;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
    NSLog(@"in didTapOverlay");
}
@end

Any ideas appreciated.

-- Bass


Solution

  • First, it will be easier if you can your camera initiation to the following:

     GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:43.5157
                                                                longitude:-114.34367
                                                                     zoom:14];
    

    So you can see your polyline easily near your within your camera.

    Then in your ViewController.h header file, change your interface to the following:

    @interface ViewController : UIViewController<GMSMapViewDelegate>
    

    So your view controller will be a GMSMapViewDelegate.

    Lastly, below your self.view = mapView_; add this following line:

    mapView_.delegate = self;
    

    This line will assign your view controller (which is a GMSMapViewDelegate) to your mapView_'s delgate, so that your mapView_'s delegate will be a GMSMapViewDelegate, and the didTapOverlay method will be called.