#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController ()
// MKMapItem 在地图App上表示一个点, 囊括地理描述, 文字描述, 商业信息
//@property (strong, nonatomic) MKMapItem *item;
@property (strong, nonatomic) CLGeocoder *coder;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 拿地方名 -> 经纬度 -> 跳转到地图上显示出来
NSString *name = @"东莞";
[self.coder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
[self jumpToMapWithPlaceMark:placemarks.lastObject];
}];
}
- (void)jumpToMapWithPlaceMark:(CLPlacemark *)mark
{
// MKPlacemark 与 CLPlacemark 表示的信息基本一致
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:mark];
// 创建 CLPlacemark 对应的MapItem
MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
/** 字典配置的Key
NSString * const MKLaunchOptionsDirectionsModeKey; // 导航
NSString * const MKLaunchOptionsMapTypeKey; // 地图类型
NSString * const MKLaunchOptionsMapCenterKey; // 地图显示区域的中心点
NSString * const MKLaunchOptionsMapSpanKey; // 地图显示区域的跨度 (矩形范围)
NSString * const MKLaunchOptionsShowsTrafficKey; // 显示交通
NSString * const MKLaunchOptionsCameraKey // 摄像头
*/
/** MKLaunchOptionsDirectionsModeKey 对应的Value
NSString * const MKLaunchOptionsDirectionsModeDriving; // 开车
NSString * const MKLaunchOptionsDirectionsModeWalking; // 走路
NSString * const MKLaunchOptionsDirectionsModeTransit; // 交通工具
*/
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.133916, 113.395557);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
/*================= 方式1 =================*/
// // 只有一个点的时候, 以用户所在位置为起点
NSDictionary *dict = @{
// MKLaunchOptionsMapTypeKey : @(MKMapTypeHybrid),
// MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
// 区域的中心点, 中心点是包装了CLCoordinate2D的结构体的NSValue
MKLaunchOptionsMapCenterKey : [NSValue valueWithMKCoordinate:coordinate],
// 区域的跨度, 1度约等于111KM
MKLaunchOptionsMapSpanKey : [NSValue valueWithMKCoordinateSpan:span],
};
// [item openInMapsWithLaunchOptions:dict];
/*================= 方式2 =================*/
// 如果指定的Item, 那么设置地图范围将无效
[MKMapItem openMapsWithItems:@[item] launchOptions:dict];
}
#pragma mark - Getter & Setter
- (CLGeocoder *)coder
{
if ( !_coder) {
_coder = [[CLGeocoder alloc] init];
}
return _coder;
}
@end
实用知识:原生地图的跳转功能
最新推荐文章于 2024-05-06 09:14:56 发布