#import "ViewController.h"
#import@interface ViewController ()
{
CLGeocoder *geocoder;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
geocoder = [[CLGeocoder alloc]init];
self.mapview.mapType = MKMapTypeStandard;
self.mapview.zoomEnabled = YES;
self.mapview.rotateEnabled = YES;
self.mapview.scrollEnabled = YES;
self.mapview.showsUserLocation = YES;
[self locateToLatitude:39.5427 longitude:116.2317];
UILongPressGestureRecognizer *gnizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)];
[self.view addGestureRecognizer:gnizer];
self.mapview.delegate = self;
}
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
// 设置地图中心的经、纬度
CLLocationCoordinate2D center = {latitude , longitude};
// 设置地图显示的范围
MKCoordinateSpan span;
// 地图显示范围越小,细节越清楚
span.latitudeDelta = 1;
span.longitudeDelta = 1;
// 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
MKCoordinateRegion region = {center,span};
// 设置当前地图的显示中心和显示范围
[self.mapview setRegion:region animated:YES];
// 创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.title = @"八维研修学院";
annotation.subtitle = @"上地7街软件园";
CLLocationCoordinate2D coordinate = {latitude , longitude};
annotation.coordinate = coordinate;
// 添加锚点
[self.mapview addAnnotation:annotation];
}
- (void) longpress:(UILongPressGestureRecognizer *)getsture{
// 获取长按点的坐标
CGPoint pos = [getsture locationInView:self.mapview];
// 将长按点的坐标转换为经度、维度值
CLLocationCoordinate2D coord = [self.mapview convertPoint:pos toCoordinateFromView:self.mapview];
// 将经度、维度值包装为CLLocation对象
CLLocation* location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
// 根据经、纬度反向解析地址
[geocoder reverseGeocodeLocation:location completionHandler: ^(NSArray *placemarks, NSError *error) {
if (placemarks.count > 0 && error == nil)
{
// 获取解析得到的第一个地址信息
CLPlacemark* placemark = [placemarks objectAtIndex:0];
// 获取地址信息中的FormattedAddressLines对应的详细地址
NSArray *addrArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
// 将详细地址拼接成一个字符串
NSMutableString* address = [[NSMutableString alloc] init];
for(int i = 0 ; i < addrArray.count ; i ++)
{
[address appendString:addrArray[i]];
}
// 创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.title = placemark.name;
annotation.subtitle = address;
annotation.coordinate = coord;
// 添加锚点
[self.mapview addAnnotation:annotation];
}
}
];
}
#if 1// MKMapViewDelegate协议中的方法,该方法的返回值可用于定制锚点控件的外观- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id) annotation
{
static NSString* annoId = @"fkAnno";
// 获取可重用的锚点控件
MKAnnotationView* annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId];
// 如果可重用的锚点控件不存在,创建新的可重用锚点控件
if (!annoView)
{
annoView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];
}
// 设置图片
annoView.image = [UIImage imageNamed:@"pos.gif"];
// 设置是否显示气泡
annoView.canShowCallout = YES;
// 定义一个按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// 为按钮绑定事件处理方法
[button addTarget:self action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
annoView.rightCalloutAccessoryView = button;
return annoView;
}
#endif
- (void) buttonTapped:(id)sender
{
NSLog(@"您点击了锚点信息!");
}
- (IBAction)Go:(id)sender {
// 关闭两个文本框的虚拟键盘
[self.latitude resignFirstResponder];
[self.longitude resignFirstResponder];
NSString* latitudeStr = self.latitude.text;
NSString* longtitudeStr = self.longitude.text;
// 如果用户输入的经度、纬度不为空
if (latitudeStr != nil && latitudeStr.length > 0
&& longtitudeStr != nil && longtitudeStr.length > 0)
{
// 调用自己实现的方法设置地图的显示位置和显示区域
[self locateToLatitude:latitudeStr.floatValue
longitude:longtitudeStr.floatValue];
}
}
@end
网友评论