1

I am a new to google map sdk for ios.I have added a map on a view.When I enter this mapView,I want to positioning myself.So I wrote :

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                        longitude:151.2086
                                                             zoom:6];
_iMapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 480) camera:camera];
self.iMapView.myLocationEnabled = YES;
self.iMapView.delegate=self;

GMSMarkerOptions *annotation = [[GMSMarkerOptions alloc] init];
annotation.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
annotation.title = @"Sydney";
annotation.snippet = @"Australia";
//annotation.infoWindowAnchor=CGPointMake(0.5, 0.5);
[self.iMapView addMarkerWithOptions:annotation];

//[self.view  addSubview:self.iMapView];
self.view=self.iMapView;

but I find the mapView view in the coordinate(33.8683,151.2086),I just want to move the mapView to the wyposition. I also find google have no callback function reference to
self.iMapView.myLocationEnabled = YES;

thank you for you reply.

Anshuman Jasrotia
  • 3,135
  • 8
  • 48
  • 81
user2143299
  • 31
  • 1
  • 3

2 Answers2

21

To animate/set the camera to your current position you first have to:

self.googleMapsView.myLocationEnabled = YES;

Then in the documentation of the GMSMapView header file you will find the following comment:

/**
* If My Location is enabled, reveals where the user location dot is being
* drawn. If it is disabled, or it is enabled but no location data is available,
* this will be nil.  This property is observable using KVO.
*/ 
@property (nonatomic, strong, readonly) CLLocation *myLocation;

So you can setup a key value observer in your viewWillAppear Method and then you get your location update with the Location Manager of the GoogleMaps SDK.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Implement here to check if already KVO is implemented.
    ...
    [self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingNew context: nil]
}

And then observe the property.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
    {
        [self.googleMapsView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.googleMapsView.myLocation.coordinate.latitude
                                                                                 longitude:self.googleMapsView.myLocation.coordinate.longitude
                                                                                      zoom:self.googleMapsView.projection.zoom]];
    }
}

Do not forget to deregister your observer in the viewWillDisappear.

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // Implement here if the view has registered KVO
    ...
    [self.googleMapsView removeObserver:self forKeyPath:@"myLocation"];
}

Best regards

Robert Weindl
  • 1,092
  • 1
  • 10
  • 30
  • 4
    NSKeyValueObservingNew --> NSKeyValueObservingOptionNew – Aurelien Porte Nov 07 '13 at 11:35
  • 1
    What an awful API from Google. KVO is bad enough as it is. This is just silly. – Adam Waite Jan 29 '14 at 16:26
  • Wanted to add another comment: for best KVO practice refer an excellent guide by NSHipster -> http://nshipster.com/key-value-observing/. It's better to use `const`s and `NSStringFromSelector` for a cleaner and less error prone implementation for example. – Adam Waite Jan 29 '14 at 16:47
  • Hi,for this googlemapview i just want to draw root on the mapview live running but i get wrong lat long .. so what i do for get perfect lat-long? – Paras Joshi Mar 06 '14 at 13:01
0

NOTE: This answer is wrong, see Robert's answer.

The Google Maps SDK for iOS doesn't have any delegate to let you know when the device's position has changed.

You would need to use the CLLocationManager class yourself, to get the device's current position, and then update the map view.

UPDATE:

To update the map view from the new CLLocation provided by the location manager, you do something like this:

GMSMapView* mapView = ...;
CLLocation* location = ...;
GMSCameraPosition* camera = [GMSCameraPosition 
    cameraWithLatitude: location.coordinate.latitude
    longitude: location.coordinate.longitude
    zoom: 6];
mapView.camera = camera;

Or if you want the map view to animate to the new location, then use this:

[mapView animationToCameraPosition: camera];
Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
  • if I use CLLocationManager to positioning myself.I get the acture coodiante of my position.The problem is to present the acture coodinate in the google mapview.is it needed correct the coodinate and present in the google mapview; – user2143299 Mar 07 '13 at 08:25
  • But how correct the coodinate(correct algorithm),and present in the google mapview – user2143299 Mar 07 '13 at 08:46
  • Hi, I'm not sure I understand your question, as the update I posted shows how to move the map view to the location provided by the location manager? – Saxon Druce Mar 07 '13 at 08:49
  • Sorry Saxon you are wrong with your answer. See my post below. – Robert Weindl Mar 08 '13 at 09:56