一行代码显示你的位置
iOS中的MapKit集成了定位的功能,使用一行代码就可以在google地图上展示出自己当前的位置,代码如下:
- -(IBAction) showLocation:(id) sender {
- if ([[btnShowLocation titleForState:UIControlStateNormal]
- isEqualToString:@"Show My Location"]) {
- [btnShowLocation setTitle:@"Hide My Location"
- forState:UIControlStateNormal];
- mapView.showsUserLocation = YES;
- } else {
- [btnShowLocation setTitle:@"Show My Location"
- forState:UIControlStateNormal];
- mapView.showsUserLocation = NO;
- }
- }
关键的代码就是:mapView.showUserLocation=YES.
使用CLLocationManager和MKMapView 还有就是通过CoreLocation框架写代码去请求当前的位置,一样也非常简单: 第一步:创建一个CLLocationManager实例- CLLocationManager *locationManager = [[CLLocationManager alloc] init];
- locationManager.delegate = self;
- locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- locationManager.distanceFilter = 1000.0f;
- locationManager.distanceFilter = kCLDistanceFilterNone;
- [locationManager startUpdatingLocation];
- [locationManager stopUpdatingLocation];
- CLLocationDegrees latitude = theLocation.coordinate.latitude;
- CLLocationDegrees longitude = theLocation.coordinate.longitude;
- CLLocationDistance altitude = theLocation.altitude;