iOS 地图
2015-10-13 21:01
344 查看
1.在程序info中添加设置并导入Libraries(CoreLocation.framework)
NSLocationWhenInUseUsageDescription 打开定位服务提高服务质量(随便写)
导入Libraries(CoreLocation.framework)
2.主页面打开地图定位服务
#import <CoreLocation/CoreLocation.h>
<CLLocationManagerDelegate>
@property (nonatomic)
CLLocationManager * locationManager;
//判断是否打开定位服务
if (![CLLocationManager
locationServicesEnabled]) {
NSLog(@"Location Service Not Enabled");
return;
}
self.locationManager = [CLLocationManager
new];
if ([CLLocationManager
authorizationStatus]==kCLAuthorizationStatusNotDetermined) {
[self.locationManager
requestWhenInUseAuthorization];
}
self.locationManager.delegate =
self;
[self.locationManager
startUpdatingLocation];
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations
{
CLLocation * location = locations[0];
NSLog(@"%@", location);
[manager stopUpdatingLocation];
}
3.创建MapViewController
#import <MapKit/MapKit.h>
<MKMapViewDelegate>
@property (weak,
nonatomic) IBOutlet
MKMapView *mapView;
@property (nonatomic)
CLPlacemark * placemark;
@property (nonatomic)
BOOL updateUserLocation;
if (self.userData.city.length<2)
{
return;
}
self.mapView.delegate =
self;
CLGeocoder * geoCoder = [CLGeocoder
new];
[geoCoder geocodeAddressString:self.userData.city
completionHandler:^(NSArray *placemarks,
NSError *error) {
self.placemark = placemarks[0];
MKPointAnnotation * ann = [MKPointAnnotation
new];
ann.coordinate =
self.placemark.location.coordinate;
ann.title =
self.userData.name;
ann.subtitle =
self.userData.city;
[self.mapView
addAnnotation:ann];
self.mapView.showsUserLocation =
YES;
}];
#pragma mark -
MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!self.updateUserLocation) {
MKCoordinateSpan span =
MKCoordinateSpanMake(fabs(userLocation.coordinate.latitude-self.placemark.location.coordinate.latitude)+1,
fabs(userLocation.coordinate.longitude-self.placemark.location.coordinate.longitude)+1);
CLLocationCoordinate2D center =
CLLocationCoordinate2DMake((userLocation.coordinate.latitude+self.placemark.location.coordinate.latitude)/2,
(userLocation.coordinate.longitude+self.placemark.location.coordinate.longitude)/2);
MKCoordinateRegion region =
MKCoordinateRegionMake(center, span);
[self.mapView
setRegion:region animated:YES];
self.updateUserLocation =
YES;
}
}
NSLocationWhenInUseUsageDescription 打开定位服务提高服务质量(随便写)
导入Libraries(CoreLocation.framework)
2.主页面打开地图定位服务
#import <CoreLocation/CoreLocation.h>
<CLLocationManagerDelegate>
@property (nonatomic)
CLLocationManager * locationManager;
//判断是否打开定位服务
if (![CLLocationManager
locationServicesEnabled]) {
NSLog(@"Location Service Not Enabled");
return;
}
self.locationManager = [CLLocationManager
new];
if ([CLLocationManager
authorizationStatus]==kCLAuthorizationStatusNotDetermined) {
[self.locationManager
requestWhenInUseAuthorization];
}
self.locationManager.delegate =
self;
[self.locationManager
startUpdatingLocation];
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations
{
CLLocation * location = locations[0];
NSLog(@"%@", location);
[manager stopUpdatingLocation];
}
3.创建MapViewController
#import <MapKit/MapKit.h>
<MKMapViewDelegate>
@property (weak,
nonatomic) IBOutlet
MKMapView *mapView;
@property (nonatomic)
CLPlacemark * placemark;
@property (nonatomic)
BOOL updateUserLocation;
if (self.userData.city.length<2)
{
return;
}
self.mapView.delegate =
self;
CLGeocoder * geoCoder = [CLGeocoder
new];
[geoCoder geocodeAddressString:self.userData.city
completionHandler:^(NSArray *placemarks,
NSError *error) {
self.placemark = placemarks[0];
MKPointAnnotation * ann = [MKPointAnnotation
new];
ann.coordinate =
self.placemark.location.coordinate;
ann.title =
self.userData.name;
ann.subtitle =
self.userData.city;
[self.mapView
addAnnotation:ann];
self.mapView.showsUserLocation =
YES;
}];
#pragma mark -
MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!self.updateUserLocation) {
MKCoordinateSpan span =
MKCoordinateSpanMake(fabs(userLocation.coordinate.latitude-self.placemark.location.coordinate.latitude)+1,
fabs(userLocation.coordinate.longitude-self.placemark.location.coordinate.longitude)+1);
CLLocationCoordinate2D center =
CLLocationCoordinate2DMake((userLocation.coordinate.latitude+self.placemark.location.coordinate.latitude)/2,
(userLocation.coordinate.longitude+self.placemark.location.coordinate.longitude)/2);
MKCoordinateRegion region =
MKCoordinateRegionMake(center, span);
[self.mapView
setRegion:region animated:YES];
self.updateUserLocation =
YES;
}
}
相关文章推荐
- iOS 创建单例的两种方法
- iOS之OC随笔-遍历数组方法总结
- iOS之CocoaPods 安装
- IOS contentMode属性
- 蓝懿iOS 技术内容和心得 10。13
- ios学习之模糊效果的学习
- iOS序列化与反序列化
- iOS之OC随笔-属性修饰符
- iOS 常用名词
- iOS第三方库
- IOS下自定义click事件使用alert引发的血案
- iOS实现地图半翻页效果--老代码备用参考
- IOS导航控制器的手势滑动后退
- iOS多线程之GCD
- ios 关于AleartView 自动取消的问题
- ios音频视频资料--备用
- iOS隐藏导航栏或返回按钮
- iOS-跨界面传值和跨应用传值
- ios coredata 老代码备用参考
- iOS 内存管理机制