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];
}
Related
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;
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:
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];
}
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
I'm trying to zoom into the user location as the center reference for the screen. I have this code:
MainViewController.h
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
IBOutlet MKMapView *mapView;
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate> {
MKMapView *mapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
MainViewController.m
#implementation MainViewController
#synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc]
initWithFrame:self.view.bounds
];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
[self.view addSubview:mapView];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = userLocation.coordinate.latitude;
location.longitude = userLocation.coordinate.longitude;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
}
Now I'm only getting a build warning on the last line [mapView setRegion:region animated:YES] stating: 'local declaration of 'mapView' hides instance variable'
When you do mapView.showsUserLocation = YES;, you ask it to retrieve the user location. This doesn't happen instantly. As it takes time, the map view notifies its delegate that a user location is available via the delegate method mapView:didUpdateUserLocation. So you should adopt the MKMapViewDelegate protocol and implement that method. You should move all your zooming-in code to this method.
Setting the delegate
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc]
initWithFrame:CGRectMake(0,
0,
self.view.bounds.size.width,
self.view.bounds.size.height)
];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
[self.view addSubview:mapView];
}
Updated delegate method
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = aUserLocation.coordinate.latitude;
location.longitude = aUserLocation.coordinate.longitude;
region.span = span;
region.center = location;
[aMapView setRegion:region animated:YES];
}
In your interface you forgot to inherit MapViewDelegate -
#import <MapKit/MapKit.h>
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate>
{
MKMapView *mapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
Rest seems fine.