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;
Related
I have a DetailViewController that is to be shown when user taps a marker on the mapView_. Initially I load the VC and set it hidden, and when the user would tap a marker, I would load values in the VC and show it. But it is not happening. Here's my code.
#property (strong, nonatomic) GMSMapView *mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
_whisperDetailView.hidden = YES;
[_whisperDetailView initlize];
_whisperDetailView.viewController = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
}
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
_detailView.whisper = marker.userData;
_detailView.hidden = NO;
return YES;
}
Storyboard snapshot:
Funny thing is I was using Mapbox before and was doing exact same thing in its didSelectAnnotation function and everything was working great. I recently had to switch to Google Map and now its not working.
Any help is appreciated. Thanks.
Okay I've got it working now! Posting the answer for anyone else stuck in same situation:
I changed the way I was adding my mapView_. instead of assigning self.view = mapView in viewDidLoad, I inserted mapView as a subview to my main view, like this:
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) camera:camera];
[self.view insertSubview:mapView_ atIndex:0];
And alas! everything works like a charm now! When I tap a marker, my DetailViewController shows with proper sizes and as I set it up in storyboard.
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.
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
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 am trying to load places inside Google Map View for iOS6.
How to set the frame for the Map ?
Currently it is fullscreen
-(void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
longitude:76.316142
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;
}
I tried by creating a new (small) view inside the current view and add map inside that,but at that time the page is not getting loaded.It shows a full black screen
-(void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
longitude:76.316142
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
[self.view Addsubview:newView];
self.newView = mapView_;
}
Finally found the solution.
Remove the loadview method, and put the following code in the view did load method:
-(void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 100, 100) camera:camera];
[self.view addSubview:mapView_];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
mapView_.myLocationEnabled = YES;
// 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_;
}
Try
mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view addSubview:mapView_];
if you put into loadView it wont have a view.. the xib isn't even loaded...
put it in viewDidLoad
and do
mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view addSubview:mapView_];
Check the declaration #property for GMSMapView , if it is weak property then make it strong.
#property (strong,nonatomic)GMSMapView *mapView;
By removing func loadView from my code fixed my problem
override func loadView() {
}
Or by putting this line of code inside the loadView
override func loadView() {
self.view = GMSMapView(frame: UIScreen.main.bounds)
}