Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current location in google map sdk in iphone

I want to set camera on current location and zoom level upto 10. So I have written code like this and for current location i get hint from this post.

How to use delegates in Google map API for IOS 6

here is my code

mapView_=[[GMSMapView alloc]initWithFrame:CGRectZero];

CLLocationCoordinate2D currentPosition = mapView_.myLocation.coordinate;

 GMSCameraPosition* camera =
[GMSCameraPosition cameraWithTarget: currentPosition zoom: 10];
 mapView_.camera = camera;
 mapView_.delegate=self;

  mapView_.mapType = kGMSTypeSatellite;
  mapView_.myLocationEnabled = YES;

  self.view = mapView_;
  mapView_.settings.myLocationButton = YES;

But coordinate is 0.00 in device.

Please Any Can help to solve out this issue?

like image 706
user2071201 Avatar asked Dec 21 '22 06:12

user2071201


2 Answers

Try this:

@property (nonatomic, retain) IBOutlet GMSMapView *googleMapView;
@property (nonatomic, retain) CLLocationManager *locationManager;

- (void)showCurrentLocation {
    _googleMapView.myLocationEnabled = YES;
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
            GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude 
                                                                    longitude:newLocation.coordinate.longitude 
                                                                         zoom:17.0];
            [_googleMapView animateToCameraPosition:camera];
        //...
}
like image 135
Stunner Avatar answered Jan 08 '23 05:01

Stunner


You can add an observer for the myLocation property as follows:

[self.mapView addObserver:self
      forKeyPath:@"myLocation"
         options:(NSKeyValueObservingOptionNew |
                  NSKeyValueObservingOptionOld)
         context:NULL];

You should then implement the following method:

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context {

    if ([keyPath isEqualToString:@"myLocation"]) {

       NSLog(@"My position changed");
    }
}
like image 38
Mannam Brahmam Avatar answered Jan 08 '23 06:01

Mannam Brahmam