【问题标题】:MKMapView or CLLocationManger - to determine user's current locationMKMapView 或 CLLocationManger - 确定用户的当前位置
【发布时间】:2015-03-27 00:49:28
【问题描述】:

有两个类有助于绘制地图和确定用户位置 - MKMapViewCLLocationManager

MKMapView 有一个代表“didUpdateUserLocation”,它告诉用户当前的位置。同时,CLLocationManger 有一个委托“didUpdateToLocation”,它也做同样的事情。

我的问题是何时使用MKMapViewCLLocationManager。我可以从MKMapView 获取设备的当前位置,那么为什么以及何时应该使用CLLocationManager?我试图得到它,但我仍然不确定。

【问题讨论】:

  • 几乎是一回事。 MapView 可能会在内部使用核心位置来查找用户的位置。如果您不需要任何用户界面但仍需要定位,请使用核心位置,否则请使用 mapview

标签: ios cocoa-touch mkmapview mapkit cllocationmanager


【解决方案1】:

我认为您将MKMapView 属性showsUserLocationCLLocationManager 混淆了。

为方便起见,MKMapView 允许您简单地启用一个属性来向用户显示地图 UI 上的当前位置。如果您只需要向用户显示他们在地图上的位置,这真的很方便。

但是,显然还有许多其他用例,仅在地图上显示位置是不够的,这就是 CLLocationManager 的用武之地。

考虑一个跑步/训练应用程序,其中需要用户位置记录来计算跑步距离,或者甚至是我自己的应用程序中的一个示例,我需要在其中找到用户位置(纬度/经度)实时计算到各个火车站的距离,以识别离用户最近的火车站。在这些示例中,不需要 MapView,因此使用 LocationManager 是正确的选择。

任何时候您都需要以编程方式与用户位置进行交互,并且基本上不需要地图 UI!

【讨论】:

    【解决方案2】:

    我更喜欢使用 MKMapView 内部使用的 CLLocationManager,所以如果您不需要使用地图,只需使用位置管理器中的以下代码即可。

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    

    只是不要忘记将 locationManager 实例存储在您的类中的某个位置,您可以像这样实现委托。

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        NSLog(@"Error detecting location %@", error);
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        CLLocation* location = (CLLocation*)locations.lastObject;
        NSLog(@"Longitude: %f, Latitude: %f", location.coordinate.longitude, location.coordinate.latitude);
    }
    

    编辑

    您可以使用 Apple 的地理编码器根据用户的位置获取用户的地址

    // Use Apple's Geocoder to figure the name of the place
        CLGeocoder* geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:location completionHandler: ^(NSArray* placemarks, NSError* error) {
            if (error != nil) {
                NSLog(@"Error in geo coder: %@", error);
            }
            else {
                if (placemarks.count == 0) {
                    NSLog(@"The address couldn't be found");
                }
                else {
                    // Get nearby address
                    CLPlacemark* placemark = placemarks[0];
    
                    // Get the string address and store it
                    NSString* locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                    location.name = locatedAt;
    
                    NSLog(@"The address is: %@", locatedAt);
                }
            }
        }];
    

    【讨论】:

    • 即使设备上的定位服务已关闭,我也需要确定设备位置。在这种情况下确定用户当前位置的正确方法是什么?
    • 如果位置服务被关闭(或者用户拒绝了你的应用的位置权限)那么你就无法确定用户的位置
    • 你不能,因为用户没有给你检测他位置的权限
    • 好的..那么有没有办法确定用户的州和国家?
    • 这是另一种情况,您可以使用 Apple Geo 编码根据用户的位置(经度和纬度)确定用户的地址,但同样您无法在未经用户许可的情况下获取位置
    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    相关资源
    最近更新 更多