4

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?

Community
  • 1
  • 1
user2071201
  • 101
  • 1
  • 2
  • 5
  • possible duplicate of [about positioning myself,some problems](http://stackoverflow.com/questions/15266118/about-positioning-myself-some-problems) – Saxon Druce May 02 '13 at 12:54

3 Answers3

8

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];
        //...
}
Stunner
  • 12,025
  • 12
  • 86
  • 145
  • Are these delegate methods, where to use this – chandru Dec 12 '13 at 11:29
  • `showCurrentLocation` is a custom method of mine that is called when the show location button is tapped in my map view. `locationManager:didUpdateToLocation:fromLocation` is a protocol method of CLLocationManagerDelegate: https://developer.apple.com/library/Mac/DOCUMENTATION/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html – Stunner Dec 12 '13 at 21:35
  • How to set animateToCameraPosition is default action when click on location button? – Linh Nguyen May 15 '15 at 07:06
3

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");
    }
}
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
  • DO NOT forget to set one of these keys in your Info.plist: `NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription` – hash3r Feb 03 '16 at 18:53
0

write this line in info plist

CLLocationManager requestAlwaysAuthorization (string) abcd

//code and also set annotation pin

.h

#import<MapKit/Mapkit.h>

CLLocationManager *locationManager;
@property(nonatomic, weak) IBOutlet MKMapView* mapView;
@property(nonatomic,retain)CLLocationManager* locationManager;


- (id)init {
self = [super init];
if(self != nil) {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
}
return self;

}

in viewDidLoad

 _mapView.showsUserLocation = YES;
_mapView.mapType = MKMapTypeStandard;
_mapView.delegate = self;


  if(IS_OS_8_OR_LATER) {
    [self.locationManager requestAlwaysAuthorization];
}

[self.locationManager startUpdatingLocation];

// NSDictionary *dictlatitude = [_dict objectForKey:@"latitude"]; float strlatitude = [[_dict objectForKey:@"latitude"] floatValue];

// NSDictionary *dictlongitude = [_dict objectForKey:@"longitude"]; float strlongitude = [[_dict objectForKey:@"longitude"] floatValue];

NSLog(@"Staring Point latitude : %f", strlatitude);
NSLog(@"Staring Point longitude: %f", strlongitude);


MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = strlatitude;
location.longitude = strlongitude;
region.span = span;
region.center = location;
[_mapView setRegion:region animated:YES];

MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title=@"name of the pin";
ann.coordinate = region.center;
[_mapView addAnnotation:ann];
Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20