ios - 打开应用程序时获取当前位置

标签 ios google-maps core-location google-maps-sdk-ios currentlocation

您好,在我的应用程序中,我必须显示从我当前位置到目的地位置的路线图。为此,我使用 google map URL 来显示路线图,一切正常。但问题是当第一次安装应用程序时它获得现金并且它没有获取当前位置。

当我最小化应用程序时,它会显示类似这样的警告消息。

"appname" would like to use Your current Location

单击“确定”后,我关闭了应用程序并再次打开它,现在它显示了路线图。

我的代码。

-(void)currentlocation{
     locationManager = [[CLLocationManager alloc]init];

     locationManager.delegate = self;


     locationManager.distanceFilter = kCLDistanceFilterNone;


     locationManager.desiredAccuracy = kCLLocationAccuracyBest;


     [locationManager startUpdatingLocation];


     [self->locationManager startUpdatingLocation];


      CLLocation *location = [locationManager location];


      CLLocationCoordinate2D coordinate = [location coordinate];

      mapView = [[[MapView alloc] initWithFrame:
            CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];

    [self.view addSubview:mapView];

   Place* home = [[[Place alloc] init] autorelease];
    home.name = @"Home";
    home.description = @"Sweet home";
    home.latitude = 13.0051850;
    home.longitude = 77.62698590;

       Place* office = [[[Place alloc] init] autorelease];
    office.name = @"Office";
    office.description = @"Bad office";
    office.latitude = coordinate.latitude;
    office.longitude = coordinate.longitude;

        [mapView showRouteFrom:home to:office];

    }

我已经使用了上面的代码,当用户打开应用程序时,它必须显示警报,一旦用户单击“确定”,它就会显示路线图,请告诉我如何实现,我一直被困在这里时间请帮帮我。

谢谢。

最佳答案

试试这个 pa..

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {

        Place* home = [[[Place alloc] init] autorelease];
        home.name = @"Home";
        home.description = @"Sweet home";
        home.latitude = 13.0051850;
        home.longitude = 77.62698590;

        Place* office = [[[Place alloc] init] autorelease];
        office.name = @"Office";
        office.description = @"Bad office";
        office.latitude = currentLocation.coordinate.latitude;
        office.longitude = currentLocation.coordinate.longitude;
        [mapView showRouteFrom:home to:office];


    }
    // Stop Location Manager
    [locationManager stopUpdatingLocation];
}

关于ios - 打开应用程序时获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256557/

相关文章:

iphone - 带有自定义layoutSubviews的UIScrollView不再有动画

ios - 当用户注销时,如何将 iOS 应用重置为初始状态

ios - 这个闭包怎么写?

android - map V2 标记按下状态

当用户在 iPhone 设置中关闭和打开位置服务时,iOS 应用程序会自动重新启动

ios - 我在 Tableview 单元格中有 collectionView。它总是返回零。我该如何解决?

java - 为什么 Google map 静态 API 无法处理我的编码 URI?

python - django 表单字段小部件中 3.35 版的 Google map API 问题

ios - iPhone 模拟器 3.2 (iPad) 中的核心位置

ios - CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用