GMSMapViewDelegate Methods Not Being Called? - ios

I am using the Google Maps iOS API. None of the delegate methods are firing. How do I make them work?
MapViewController.h:
#interface MapViewController : UIViewController <GMSMapViewDelegate>
#property (strong, nonatomic) IBOutlet GMSMapView *mapView_;
#end
#import "MapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface MapViewController ()
#end
MapViewController.m
#implementation MapViewController
#synthesize mapView_;
GMSCircle *circ;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)loadView {
mapView_.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10
longitude:10
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
mapView_.mapType = kGMSTypeHybrid;
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(10, 10);
GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
radius:10];
circ.tappable = true;
[circ setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:.5]];
circ.map = self.mapView_;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
NSLog(#"in didTapOverlay");
[overlay isKindOfClass:[circ class]];
if ([overlay isKindOfClass:[circ class]]) {
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(10, 10);
marker.title = #"Place";
marker.snippet = #"Sub Place";
marker.map = mapView_;
}
}
- (void) mapView:(GMSMapView *) mapView willMove:(BOOL) gesture
{
NSLog(#"In willMove");
}
#end

I think you are setting the delegate property to early
try this :
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10
longitude:10
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
self.view = mapView_;

Set the delegate in the ViewDidLoad

Related

While Zooming and Pinching map , Google map is moving

I have referred all the below mentioned but nothing helped me. My requirement is if we double tab or zoom in or zoom out ,map and entered image should not move like Uber,Carrem.I am new to IOS. I have placed the code,Can anyone help me,
Zoom in the center of the screen - Google Maps iOS SDK Swift
Google Maps SDK iOS - prevent map from changing location on zoom
How to setCenter mapview with location in google maps sdk for iOS
#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface ViewController ()<UIGestureRecognizerDelegate,CLLocationManagerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//To hide the default gestures
self.mapView.settings.allowScrollGesturesDuringRotateOrZoom=NO;
//self.mapView.settings.zoomGestures=NO;
//self.mapView.settings.rotateGestures=NO;
//After loading page camera will point this.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:24.840216
longitude:46.676199
zoom:14];
[self.mapView animateToCameraPosition:camera];
self.mapView.settings.myLocationButton=TRUE;
//Make the image as center of the map
UIImageView *pin =[[UIImageView alloc]init];
pin.frame=CGRectMake(0, 0, 40, 40 );
pin.center = self.mapView.center;
pin.image = [UIImage imageNamed:#"location.png"];
[self.view addSubview:pin];
[self.view bringSubviewToFront:pin];
UIPanGestureRecognizer *doubleTab = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
doubleTab.delegate=self;
//doubleTab.numberOfTapsRequired=2;
[self.mapView addGestureRecognizer:doubleTab];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) didPan:(UIPanGestureRecognizer*) gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(#"YES");
self.mapView.settings.scrollGestures = YES;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer.numberOfTouches > 1)
{
NSLog(#"gestureRecognizer:NO");
self.mapView.settings.scrollGestures = NO;
}
else
{
NSLog(#"gestureRecognizer:YES");
self.mapView.settings.scrollGestures = YES;
}
return true;
}
#end
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
longitude:103.848
zoom:12];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.settings.scrollGestures = NO;
mapView.settings.zoomGestures = NO;
self.view = mapView;

Custom infobox when tap event

I'm trying to have an info box, same one as the standard for markers, to appear when a my polyline is tapped. I've gotten a NSLog to output when the line is tapped, but now I need to have the infobox appear instead of the NSLog. I've seen some Javascript examples but no objective c ones.
- (void)loadView {
// Create a GMSCameraPosition
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.551927
longitude:-77.456292
zoom:18];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(37.551709, -77.456510);
mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
self.view = mapView;
mapView.delegate = self;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(37.552243, -77.457415)];
[path addCoordinate:CLLocationCoordinate2DMake(37.551054, -77.455443)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
UILabel *myLabel = [[UILabel alloc] init];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
polyline.spans = #[[GMSStyleSpan spanWithColor:[UIColor greenColor]]];
polyline.strokeWidth = 5.f;
polyline.tappable = true;
polyline.map = mapView;
}
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
NSLog(#"in didTapOverlay");
}
#end
Check the Events guide from the iOS Maps tutorial.
Here's a snippet in Objective-C:
- (void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
longitude:103.848
zoom:12];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.delegate = self;
self.view = mapView;
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
Here's an SO thread demonstrating the use of GMSMapViewDelegate:
1.conform to the GMSMapViewDelegate protocol.
#interface YourViewController () <GMSMapViewDelegate>
// your properties
#end
2.set your mapView_ delegate.
mapView_.delegate = self;
3.implement the GMSMapViewDelegate method
- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {
// your code
}
Addition: marker.userData is useful. you can set your needed data into it and use it in - mapView:didTapInfoWindowOfMarker:

ios google maps delegate events not firing in simulator

For some reason, I can't seem to get tap and longtap and all the other delegate events to work in my simulator. I think I installed everything correctly because the map shows up on the screen, with a custom button click I can put a marker on the map, but it will not recognize the delegate events. Am I doing something wrong???
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
The only thing that works is the IBAction. Also my location and compass buttons do not show up. Any ideas of why it's not working? I declared the delegates...
You need to set all properties after the object initialization.
- (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];
}

iOS Factory design patteren

I implementing Google maps with factory design pattern. But map is not displayed when i load the map view. When i implemented the same without using factory pattern, i could get it loaded successfully. please help me to fix this issue. Below shown is the code.
//Caller
#import "ViewController.h"
#import "Constants.h"
#import "MapBuilderFactory.h"
#import "MapBuilderDelegate.h"
- (void)viewDidLoad
{
[super viewDidLoad];
id<MapBuilderDelegate> mapBuilder=[MapBuilderFactory mapWithName:GoogleMaps];
[mapBuilder initMapWithApiKey:kGoogleMapsApiKey];
UIView *mapView= [mapBuilder mapView];
[self.view addSubview:mapView];
}
Implementation of MapBuilderFactory
#import "MapBuilderFactory.h"
#import "GoogleMapsViewController.h"
#implementation MapBuilderFactory
+(id)mapWithName:(mapType)mapType
{
id returnValue;
switch (mapType) {
case AppleMaps:
returnValue=nil;
break;
case GoogleMaps:
returnValue=[GoogleMapsViewController new];
break;
default:
break;
}
return returnValue;
}
#end
Implementation of GoogleMapsViewController
#interface GoogleMapsViewController ()
#property(nonatomic,retain)GMSMapView *mapView;
#end
#implementation GoogleMapsViewController
#synthesize mapView=mapView_;
-(void)initMapWithApiKey:(NSString*)apiKey
{
[GMSServices provideAPIKey:apiKey];
}
-(UIView*)mapView
{
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
return mapView_;
}
MapBuilderDelegate
#protocol MapBuilderDelegate <NSObject>
-(void)initMapWithApiKey:(NSString*)apiKey;
-(UIView*)mapView;
#end
is the problem with the view or is the map configuration? I dont see where the frame is specified. below i've added a setframe in your viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
id<MapBuilderDelegate> mapBuilder=[MapBuilderFactory mapWithName:GoogleMaps];
[mapBuilder initMapWithApiKey:kGoogleMapsApiKey];
UIView *mapView= [mapBuilder mapView];
[mapView setFrame:CGRectMake(0.0, 0.0, 320.0, 500.0)];
[self.view addSubview:mapView];
}

How to add a toolbar over GMSMapView

I am making an iPhone application that uses a GMSMapView and I want to be able to add a toolbar on top of the map. This is my code:
#import "MapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface MapViewController ()
#end
#implementation MapViewController
{
GMSMapView *mapView_;
id<GMSMarker> myMarker;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
// You don't need to modify the default initWithNibName:bundle: method.
- (void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:30.616083 longitude:-96.338908 zoom:13];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
self.view = mapView_;
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(30.616083, -96.338908);
options.title = #"College Station";
options.snippet = #"Texas";
[mapView_ addMarkerWithOptions:options];
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
[myMarker remove];
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarkerOptions *marker = [[GMSMarkerOptions alloc] init];
marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
marker.title = #"Tap Event";
// (icon for marker) marker.icon = [UIImage imageNamed:#"house"];
myMarker = [mapView_ addMarkerWithOptions:marker];
}
#end
In my storyboard i have a toolbar but I do not know how to get it to be displayed over the GMSMapView. Im new to iOS and have searched for solutions online but haven't had any luck with GMSMapView.
Because you set the view to the mapView_, the view made in StoryBoard is overriden. I've added a toolbar with an Segmented Control and an button using the following code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, screenBounds.size.width, 44)];
UINavigationItem *navItems = [[UINavigationItem alloc]init];
UIBarButtonItem *barButtonRight = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:#selector(SettingsButtonPressed)];
UISegmentedControll *segControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"Opt1",#"Opt2",#"Opt3", nil]];
UIImage *imageSettings= [UIImage imageNamed:#"36-toolbox.png"];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setSelectedSegmentIndex:0];
[segControl setFrame:CGRectMake(10, 5, 150, 32)];
[segControl addTarget:self action:#selector(KiesVC:) forControlEvents:UIControlEventValueChanged];
[barButtonRight setImage:imageSettings];
[navItems setRightBarButtonItem:barButtonRight];
[navItems setTitleView:segControl];
[navBar setItems:[NSArray arrayWithObject:navItems] animated:NO];
You then add the toolbar by calling :
[self.view addSubview:navBar];
Hope it helps

Resources