iOS Factory design patteren - ios

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];
}

Related

How to show india map on google maps in objective c

I am new in iOS and I am facing problem regarding to show India maps on Google Maps
My code is like this first
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726
longitude:73.856255
zoom:3];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//Scrollview
mapView = [GMSMapView mapWithFrame:CGRectMake(10, 140, 750, 350) camera:camera];
}
else
{
//Scrollview
mapView = [GMSMapView mapWithFrame:CGRectMake(10, 140, 550, 350) camera:camera];
}
But When I used UIView and create it using IBOutlet like this
IBOutlet GMSMapView *mapView;
Than the above code not work.How can I show the India map first by using above coordinate.Thanks in Advance!
Get current Location coordinate and Pass on these camera position
CLLocationCoordinate2D coordinate;
float latitude;
float longitude;
coordinate = [self getLocation];
latitude = coordinate.latitude;
longitude = coordinate.longitude;
//pragma mark ----: GET Coordinate2D :----
-(CLLocationCoordinate2D) getLocation
{
CLLocation *location = [_locationManager location];
coordinate = [location coordinate];
return coordinate;
}
CGRect frame = CGRectMake(0, 0, kSCREEN_WIDTH,kSCREEN_HEIGHT);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude longitude:longitude zoom:12];
mapView_ = [GMSMapView mapWithFrame:frame camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
[mapView_ setCamera:camera];
Below is a Sample Code:
#import <GoogleMaps/GoogleMaps.h>
#interface ViewController ()<GMSMapViewDelegate>
{
GMSMapView *mapView;
GMSMarker *marker;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self showMarker];
}
- (void)showMarker
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726 longitude:73.856255 zoom:3];
mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view addSubview:mapView];
}
#end
Try This code:
#interface ViewController ()<GMSMapViewDelegate>
{
GMSMapView *mapView_;
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726
longitude:73.856255
zoom:3];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//Scrollview
mapView_ = [GMSMapView mapWithFrame:CGRectMake(10, 140, 750, 350) camera:camera];
}
else
{
//Scrollview
mapView_ = [GMSMapView mapWithFrame:CGRectMake(10, 140, 550, 350) camera:camera];
}
NSError *error;
// Set the map style by passing a valid JSON string.
GMSMapStyle *style = [GMSMapStyle styleWithJSONString:kMapStyle error:&error];
if (!style) {
NSLog(#"The style definition could not be loaded: %#", error);
}
mapView_.mapStyle = style;
mapView_.mapType = kGMSTypeNormal;
[self.scrllView addSubview:mapView_];
[_btnMap setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
mapView_.delegate = self;
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
18.516726,
73.856255);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView_;
marker.icon=[UIImage imageNamed:#"locationBlue"];
marker.title = #"From";

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];
}

I can't see map after adding google map sdk for ios

I want to add a google map above a UIViewController. But I can't see anything except some markers I defined. The problem looks like this:
And here is the related code:
- (void)viewDidLoad {
previousContext = [EAGLContext currentContext];
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:10];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 1136, 640) camera:camera];
//mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.delegate = self;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
[EAGLContext setCurrentContext: previousContext];
//add makers
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(39.900285, 116.274020);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"aaa";
marker.snippet = #"population : 5";
marker.infoWindowAnchor = CGPointMake(0.5, 0.5);
marker.map = mapView_;
marker.icon = [UIImage imageNamed:#"tempmarker.png"];
CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(39.860285, 116.274020);
GMSMarker *marker2 = [GMSMarker markerWithPosition:position2];
marker2.title = #"bbb";
marker2.snippet = #"population : 5";
marker2.infoWindowAnchor = CGPointMake(0.5, 0.5);
marker2.map = mapView_;
marker2.icon = [UIImage imageNamed:#"tempmarker2.png"];
//add buttons:directRoom, rank, achievement, mission, home
....
[self.view insertSubview: mapView_ atIndex: 0];
[EAGLContext setCurrentContext:nil];
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
}
So, how to deal with this problem, thanks a lot!
For reference, see the demo from Google (copied below). It's similar to what you posted, but you can try a couple of things:
make sure your API key is valid and the API can fetch tiles
check the z-index ordering of the map view you're inserting: [self.view insertSubview: mapView_ atIndex: 0]; inserts behind everything else. Try [self.view addSubview:mapView_]
Demo code, from Google (https://developers.google.com/maps/documentation/ios/):
#import <GoogleMaps/GoogleMaps.h>
#import "DemoViewController.h"
#implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:6];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.animated = YES;
self.view = mapView;
}
#end
Check whether you have entered the google API key correctly in the didFinishLaunchingWithOption in your app delegate
[GMSServices provideAPIKey:#"yourGoogleAssignedSDKKeyGoesHere"];
And your xcode should be 4.5 or later

GMSMapViewDelegate Methods Not Being Called?

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

Resources