Search code examples
iosobjective-cgoogle-mapsgoogle-maps-sdk-ios

How to Show User Current Location On Zoom When View Did Load in iOS?


I am newbie in iOS Development i make an Goggle map Supported app it Shows User Current Location but When i Press CurrentLocation Button on my GMSMapView then i show user Current Location but i want to Show User to Current Location WithOut Button Press i mean When viewDidLoad i Show Direct Current Location with Zoom Please Help me.

Here i Write a code like as in my .h File.

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>

@interface MapViewController : UIViewController<GMSMapViewDelegate>
@property (nonatomic,retain) IBOutlet GMSMapView *mapView;
@end

And Code for .m File like as

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.myLocationEnabled = YES;
    self.mapView.mapType = kGMSTypeNormal;
    self.mapView.settings.compassButton = YES;
    self.mapView.settings.myLocationButton = YES;
    self.mapView.delegate = self;
}

Please help me For Show Current location without Current location Button Press. And Please Help me How to Get Current Location Address in Google map.

thanks.


Solution

  • Try:

    [self.mapView animateToLocation: self.mapView.myLocation.coordinate];
    

    Keep in mind that myLocation might be nil if the location is not determined yet! You could use Key-Value Observing (KVO) to determine when to animate to current location.

    -(void) viewDidLoad {
        [super viewDidLoad];
        [self.mapView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    }
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
        if ([keyPath isEqualToString:@"myLocation"]) {
            // Animate map to current location (This will run every time user location updates! 
            [self.mapView animateToLocation: self.mapView.myLocation.coordinate];
            // You can remove self from observing 'myLocation' to only animate once
            [self.mapView removeObserver:self forKeyPath:@"myLocation"];
        }
    }