简体   繁体   English

MKMapView或CLLocationManger-确定用户的当前位置

[英]MKMapView or CLLocationManger - to determine user's current location

There are two classes that help regarding map plotting and determining user location - MKMapView and CLLocationManager . 有两个类可帮助您进行地图绘制和确定用户位置MKMapViewCLLocationManager

MKMapView has a delegate “ didUpdateUserLocation ” that tells the user's current location. MKMapView有一个委托“ didUpdateUserLocation ”,它告诉用户当前的位置。 At the same time, CLLocationManger has a delegate “ didUpdateToLocation ” and it also does the same thing. 同时, CLLocationManger有一个委托“ didUpdateToLocation ”,它也做同样的事情。

My question is when to use MKMapView and CLLocationManager . 我的问题是何时使用MKMapViewCLLocationManager I am able to get current location of the device from MKMapView then why and when should I use CLLocationManager ? 我可以从MKMapView获取设备的当前位置,然后为什么以及何时应该使用CLLocationManager I tried to get it but I am still not sure. 我试图得到它,但我仍然不确定。

I think you are confusing the MKMapView property showsUserLocation with CLLocationManager . 我认为您将MKMapView属性showsUserLocationCLLocationManager混淆了。

As a convenience MKMapView's allow you to simply enable a property to show the users current location on the map UI. 为方便起见,MKMapView允许您简单地启用一个属性以在地图UI上向用户显示当前位置。 This is really handy if you only need to show the user where they are on a map. 如果只需要向用户显示他们在地图上的位置,这将非常方便。

However, there are demonstrably many other use cases where simply showing location on a map is not enough and this is where CLLocationManager comes in. 但是,显然还有许多其他用例,仅在地图上显示位置是不够的,这就是CLLocationManager用武之地。

Consider for example a running/training application, where a record of user locations is required to calculate running distance, or even an example from one of my own applications, where by I needed to find the users location (lat/long) to calculate distance to various train stations in real time to identify which was closest for the user. 例如,考虑一个跑步/训练应用程序,其中需要记录用户位置才能计算跑步距离,甚至考虑我自己的一个应用程序中的示例,在该应用程序中我需要查找用户位置(经/纬度)来计算距离实时访问各个火车站,以识别最接近用户的位置。 In these examples there is no need for a MapView so using a LocationManager is the right choice. 在这些示例中,不需要MapView,因此使用LocationManager是正确的选择。

Anytime you need to programmatically interact with the users location and don't require a map UI basically! 任何时候您需要以编程方式与用户位置进行交互,并且基本上不需要地图UI!

I prefer to use CLLocationManager which is used internally by the MKMapView, so if you don't need to use the map, just use the below code from the location manager. 我更喜欢使用MKMapView内部使用的CLLocationManager,因此,如果不需要使用地图,只需使用位置管理器中的以下代码。

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

Just don't forget to store the locationManager instance somewhere in your class and you can implement the delegate like this. 只是不要忘记将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);
}

EDIT 编辑

You can get the address of the user based on their location using Apple's Geo coder 您可以使用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);
            }
        }
    }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM