Google Maps SDK not displaying properly in UIView? - ios

I'm having some problems displaying google maps in Xcode 6.1
I managed to get the map to display in a section of my UIView, but it displays the world map instead of at a particular coordinate.
Here is the issue:
.h:
#interface MapViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *googleMapView;
- (IBAction)mapToMain:(UIButton *)sender;
#end
.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134
longitude: -117.140269 zoom: 17];
GMSMapView *mapView = [GMSMapView mapWithFrame:self.googleMapView.bounds
camera:camera];
mapView.myLocationEnabled = YES;
self.googleMapView = mapView;
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
marker.title = #"Tet Festival 2015";
marker.snippet = #"VAYA Tet";
marker.map = mapView;
}
I saw some suggestions here, but it didn't work:
Cannot put a google maps GMSMapView in a subview of main main view?
Using the Google Maps SDK in views other than the main view
Any suggestions?

You have to add a UIView to your Storyboard... but after that you have to turn this simple UIView into a GMSMapView! Select the view you just added and open the Identity Inspector by selecting the third tab from the left in the Utilities toolbar, change the view’s class name from UIView to GMSMapView, as shown in the screenshot below:
Your Outlet will not be like this
#property (strong, nonatomic) IBOutlet UIView *googleMapView;
it will be like this
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
Your ViewDidLoad can be like this:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134
longitude: -117.140269 zoom: 17];
[self.mapView animateToCameraPosition:camera];
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
marker.title = #"Tet Festival 2015";
marker.snippet = #"VAYA Tet";
marker.map = self.mapView;
UPDATE
Here you have an example project (insert your Key)

Finally figured it out...
removed:
self.googleMapView = mapView;
replaced:
[self.googleMapView addSubview:mapView];

I am not sure what is wrong with the code above. It looks fine for me. But to move the camera to certain location. You can animate the camera there as well.
var newLocation = CLLocationCoordinate2DMake(latitude, longitude)
googleMapView.animateToLocation(newLocation)
Hope that it works for you.

Related

UIView Black Screen in Google Maps iOS

After joining the Google Maps project covers the default application screen, which are already added objects. Trying to implement it to a UIView application screen turns black.
I would like to add it to the element UIView.
Please help.
.h
#property (weak, nonatomic) IBOutlet UIView *mapViewWithFrame;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *btnLocate;
#property (weak, nonatomic) IBOutlet GMSMapView *mapView_;
.m
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:54.104382
longitude:22.9403031
zoom:15];
self.mapView_ = [GMSMapView mapWithFrame:self.mapView.bounds camera: camera];
self.mapView_.myLocationEnabled = YES;
// Add as subview the mapview
[self.mapViewWithFrame addSubview: self.mapView_];
I am also get the same issue. im using this code getting correctly, try this
1) Add subview in uiviewcontroller
2) Set custom class name in "GMSMapView" in identity inspector
3) add .h file in this code
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
4) in .m file add this code in viewDidLoad
-(void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 11.066853 longitude: 76.9460043 zoom: 17];
[self.mapView animateToCameraPosition:camera];
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(11.066853, 76.9460043);
marker.title = #"Tet Festival 2015";
marker.snippet = #"VAYA Tet";
marker.map = self.mapView;
self.mapView.mapType = kGMSTypeNormal;
}
5) uiviewcontroller should mapping the uiview in "GMSMapView" this object

do Google Map Markers have to be added in ViewDidLoad?

This is my code for ViewController.m: So far, I've been loading a list of Map markers from a database in viewDidLoad. I want to add a marker and have it appear on the map, but the marker never shows up until I close the view and open it again.
#interface ViewController : UIViewController <GMSMapViewDelegate, CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
#end
- (void)viewDidLoad {
[super viewDidLoad];
// Getting My Location
//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy: kCLLocationAccuracyBest];
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
NSLog(#"created locationManager");
// Google Map View
_mapView.settings.compassButton = YES;
_mapView.padding = UIEdgeInsetsMake(self.topLayoutGuide.length + 10, 0, self.bottomLayoutGuide.length, 0);
/* Setting Up Markers */
self.mapView.delegate = self;
[self addMarkers];
}
an IB Action is called (and definitely gets called) to add a new Marker, but the Marker does not show up on the map. I've tried this in ViewWillAppear and others, but the marker is not added. I'm starting to think markers can only be added in ViewDidLoad...is this true?
-(IBAction)unwindtoRoot:(UIStoryboardSegue *)segue {
NSLog(#"unwindToRoot");
//[self addMarkers];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(40.729358, -73.998301);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"Kopi Kopi";
marker.map = _mapView;
}

Unable to navigate Google Map to location

I am using Google Maps in iOS. I have added map in subview. But animateToLocation method doesn't work although it works fine if I assign mapView to whole view with
self.view = mapView_
[mapView_ animateToLocation:CLLocationCoordinate2DMake(-33.868, 151.208)];
I found solution by change outlet type GMSMapView and setting camera in viewDidLoad method.
in .h file
#property (weak, nonatomic) IBOutlet GMSMapView *mapView;
in viewDidLoad
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:131.20 zoom:6];
self.mapView.camera = camera;
You can try out the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:131.20
zoom:6];
self.map1.camera = camera;

iOS Google maps sdk - markers are not pointed while using subview instead of main view

Trying to display a simple Google map with a marker at location "position".
If mapview_ is shown in self.view(Default view of controller) instead of gmapsView (subview of self.view) of class GMSMapView then everything is working fine.
I have gone through some of the posts on SO but couldn't solve the issue.
in Interface builder gmapsview class is set to GMSMapView.
Camera Postion setup
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
37.778376,
-122.409853);
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:position.latitude longitude:position.longitude];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:position.coordinate.latitude
longitude:position.coordinate.longitude
zoom:13];
Map setup
mapView_ = [GMSMapView mapWithFrame:self.gmapsView.frame camera:camera];
mapView_.delegate = self;
self.gmapsView.camera = camera; //gmapsView is a view of class GMSMapView
CGRect rect = self.locationBlockView.frame;
self.gmapsView.delegate = self; //Added to check whether it works
self.gmapsView = mapView_;
Setting up Marker
GMSMarker *marker = [GMSMarker markerWithPosition:position]; //Adding Marker
marker.map = mapView_;
[mapView_ animateToLocation:position.coordinate];
Re-Initializing the map was causing the issue. Once it is removed it solved the issue. But Re-initializing GMSMapView is causing the issue ? Its an absurd thing, but it solved the issue.
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
12.778376,
-122.409853);
currentLocation = [[CLLocation alloc] initWithLatitude:position.latitude longitude:position.longitude];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:eventLocation.coordinate.latitude
longitude:eventLocation.coordinate.longitude
zoom:13];
//[self.gmapsView removeFromSuperview];//removing the subview
//self.gmapsView = [GMSMapView mapWithFrame:self.gmapsView.frame camera:camera];
//self.gmapsView.delegate = self;
//self.gmapsView = self.gmapsView; //set map
// [self.view addSubview:self.gmapsView];//Adding back did the magic
self.gmapsView.delegate = self; //set delegate
self.gmapsView.camera = camera; //set camera
GMSMarker *marker = [GMSMarker markerWithPosition:position]; //Adding Marker
marker.map = self.gmapsView;
[self.gmapsView animateToLocation:currentLocation.coordinate];
Add Google Map To Subview
The Solution Steps
1) Change the class of the UIView in the story to GMSMapVIew.
2) Next I created an IBOutlet for the green UIView container and Connected it.
#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
#interface CarLocationViewController : UIViewController
//An outlet of type GMSMapView
#property (strong, nonatomic) IBOutlet GMSMapView *mapContainerView;
#end
3) Then in my view did load I did the following
- (void)viewDidLoad {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:14];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = self.mapContainerView;
//set the camera for the map
self.mapContainerView.camera = camera;
self.mapContainerView.myLocationEnabled = YES;
}
I am Following This Link http://www.ryanwright.me/cookbook/ios/obj-c/maps/gmap/subview

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

Resources