IOS高德地圖

#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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容