ghostrider
ghostrider

Reputation: 5259

GMSMapView always display the same location iOS

as the title says no matter what coordinates I give I see the same location.

Here is my code:

I am using storyboard and I have a subview inside my view. The subview is of type GMSMapView, and correctly linked.

.h

@property (strong, nonatomic) IBOutlet GMSMapView *mymap;

.m

@implementation DealViewController
{
    GMSMapView *mapView_;
}


@synthesize mymap=_mymap;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view layoutIfNeeded];

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                            longitude:151.2086
                                                                 zoom:6];
    mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];
    mapView_.myLocationEnabled  = YES;
    mapView_.mapType = kGMSTypeSatellite;
    mapView_.delegate = self;
    self.mymap=mapView_;

}

I also tried this, but I get the same:

mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

What is wrong with the above?

Upvotes: 1

Views: 7500

Answers (4)

Jon Vogel
Jon Vogel

Reputation: 5634

GMSMapView seems to struggle fitting to GMSCoordinateBounds in these situations:

1) You init the GMSMapView with a .zero frame

2) The GMSMapView is not yet visible on the screen

3) The GMSMapView is a subview of any given UIViewController

In any of these three cases you have to set the camera explicitly:

let camera = GMSCameraPosition.camera(withTarget: B.center, zoom: 19)
self.map.camera = camera

Upvotes: 0

Dmitry Sikorsky
Dmitry Sikorsky

Reputation: 1501

After hours of cruel research of the same problem I've found that viewDidLoad method should looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:obj.latitude
                                 longitude:obj.longitude
                                 zoom:12];
    self.mapView.camera = camera;
}

In other words in this case you should NOT assign any value (like '[GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];') to self.mapView, just set it's property 'camera' value.

This works for me. I hope this will save time for someone else too.

Upvotes: 11

skarE
skarE

Reputation: 5890

If you are using storyboards make sure you have set the type of the view to be a GMSMapView in the storyboard UI.

Then all you should need to do is:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view layoutIfNeeded];

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                            longitude:151.2086
                                                                 zoom:6];
    self.mymap.camera = camera;
    self.mymap.myLocationEnabled  = YES;
    self.mymap.mapType = kGMSTypeSatellite;
    self.mymap.delegate = self;
}

There is a storyboard example that is available on the Google Maps github page.

Upvotes: 5

lxt
lxt

Reputation: 31304

I think this is your problem:

@property (strong, nonatomic) IBOutlet GMSMapView *mymap;

@implementation DealViewController
{
    GMSMapView *mapView_;
}

@synthesize mymap=_mymap;

mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];

Your map is coming in from your storyboard, called mymap. You create an instance variable called mapView_ but you synthesize mymap to _mymap...which is different.

Rather than using an instance variable, just access the property directly. Replace all the mapView_ instances with self.mymap.

For example:

self.mymap = [GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];

As to why it doesn't move to the user's location - here's what the Google Maps iOS SDK documentation has to say about the myLocationEnabled property:

myLocationEnabled - Controls whether the My Location dot and accuracy circle is enabled.

...what it doesn't do is move the map to the current user's location. It only shows the user's location dot and circle on the map.

Your map is always displaying the same location because you never change it. You create a camera set to (-33.8683, 151.2086), ask the map to show the user's location indicator, but don't ask the map to change its location to that of the user.

To do this you can use the map's myLocation property to obtain the current location of the user, and then the animateToLocation method to move the map accordingly.

Upvotes: 0

Related Questions