1

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?

ghostrider
  • 5,131
  • 14
  • 72
  • 120
  • is this issue resolved? if you are seeing a location in Europe(showing UK, France, etc), then it is the default map location that is shown when the map is initialised as `mapView_ = [[GMSMapView alloc] initWithFrame:CGRectZero];` without mentioning any camera position. So your camera position setting is not working. – tony m Jul 26 '13 at 05:02
  • @tony not it is not resolved, but I used the default map kit in IOS instead of google maps. Thanks, I will check it out. – ghostrider Jul 26 '13 at 09:58
  • check is this [earlier post](http://stackoverflow.com/questions/15059123/using-the-google-maps-sdk-in-views-other-than-the-main-view) is useful – tony m Jul 27 '13 at 10:41
  • did you find an answer? – Efren Jul 27 '17 at 23:47

4 Answers4

11

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.

Dmitry Sikorsky
  • 1,374
  • 14
  • 23
5

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.

skarE
  • 5,880
  • 2
  • 23
  • 23
0

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.

lxt
  • 31,146
  • 5
  • 78
  • 83
  • in the google maps documentation, when the above code is used (ignore the myLocationEnabled) it should give me the area around Singapore and not in Europe. No matter what coordinates I give, the camera gives me the same location (centred somewhere around Europe) and I do not see the marker at all. – ghostrider Jul 02 '13 at 14:29
  • Sydney with those coords, surely, not Singapore? – lxt Jul 02 '13 at 14:33
  • yes in the above example, I should see Sydney. i have tried also with Singapore's but in both cases I see an area in Europe with no marker. – ghostrider Jul 02 '13 at 14:35
  • I think I've figured it out. Your instance variable synthesis isn't right. I'm going to edit my answer above with some more info at the top. – lxt Jul 02 '13 at 14:37
  • thanks for the try but unfortunately nothing changes. If it makes any difference, I have also added code for identifying the tapping but this is not working as well, i do not see the log messages. It is like my map is not responding and just give me the default mapview. – ghostrider Jul 02 '13 at 14:59
0

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
Jon Vogel
  • 5,244
  • 1
  • 39
  • 54