user3599133
user3599133

Reputation: 155

how to put current location coordinate to google maps api (camera)

I am using google map api in my app and I want when run my code show me my location automatic in view. I write my code and run but my code not working and I understand that my location method don't save my coordinate location in two variable.why???

please guide me about it.

    @implementation ViewController
    {
        double latitudes;
        double longitudes;
        CLLocationManager *locationManager;
        GMSMapView *mapView_;
    }
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        locationManager = [[CLLocationManager alloc] init];
        [self GetMyLocation];

        // Create a GMSCameraPosition that tells the map to display the

//my friend I don't know why my two variable (latitudes,longitudes) 
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitudes longitude:longitudes zoom:14];
        mapView_ = [GMSMapView mapWithFrame:CGRectZero  camera:camera];
        mapView_.myLocationEnabled = YES;
        [mapView_ setMapType:kGMSTypeNormal];
    }
    - (void) GetMyLocation{
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
    }
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        CLLocation *currentLocation = newLocation;
        if (currentLocation != nil) {
            longitudes = currentLocation.coordinate.longitude;
            latitudes = currentLocation.coordinate.latitude;
        }
    }
    @end

Upvotes: 1

Views: 2150

Answers (1)

Shams Ahmed
Shams Ahmed

Reputation: 4513

 @implementation ViewController
    {
        double latitudes;
        double longitudes;
        CLLocationManager *locationManager;
        GMSMapView *mapView_;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        locationManager = [[CLLocationManager alloc] init];
        [self GetMyLocation];

        // Create a GMSCameraPosition that tells the map to display the

//my friend I don't know why my two variable (latitudes,longitudes) 
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:0 longitude:0 zoom:14];
        mapView_ = [GMSMapView mapWithFrame:self.view.frame  camera:camera];
        mapView_.myLocationEnabled = YES;
        [mapView_ setMapType:kGMSTypeNormal];
        [self.view addSubView:mapView_];
    }

    - (void) GetMyLocation{
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
    }

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        CLLocation *currentLocation = newLocation;
        if (currentLocation != nil) {
            longitudes = currentLocation.coordinate.longitude;
            latitudes = currentLocation.coordinate.latitude;
        }

        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:currentLocation.coordinate.longitude longitude:currentLocation.coordinate.latitude zoom:14];

        [mapView_ animateToCameraPosition:camera];
    }

    @end

Upvotes: 2

Related Questions