Alexyuiop Alexyuiop - 1 year ago 158
iOS Question

Google Maps API: Getting coordinates of current location iOS

I am currently working with Google Maps API in my project. I am trying to set the default camera/zoom to the users location. I do this:

@implementation ViewController{

GMSMapView *mapView_;

@synthesize currentLatitude,currentLongitude;

- (void)viewDidLoad
[super viewDidLoad];
mapView_.settings.myLocationButton = YES;
mapView_.myLocationEnabled = YES;

- (void)loadView{

CLLocation *myLocation = mapView_.myLocation;

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = @"Current Location"; = mapView_;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

self.view = mapView_;
NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);


However, it does not work, since when I do

NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);

it returns 0, 0, and it does not give the current location coordinates. How can I properly get the user's coordinates?

Answer Source


#import <CoreLocation/CoreLocation.h>
@property(nonatomic,retain) CLLocationManager *locationManager;


- (NSString *)deviceLocation 
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
return theLocation;

- (void)viewDidLoad
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

answered here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download