導(dǎo)入4個(gè)包
拖拽屬性
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UITextField *longitudeField;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)Go:(id)sender;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 設(shè)置地圖的顯示風(fēng)格
self.mapView.mapType = MKMapTypeStandard;
}
- (IBAction)Go:(id)sender {
// 關(guān)閉兩個(gè)文本框的虛擬鍵盤
[self.latitudeField resignFirstResponder];
[self.longitudeField resignFirstResponder];
NSString* latitudeStr = self.latitudeField.text;
NSString* longtitudeStr = self.longitudeField.text;
// 如果用戶輸入的經(jīng)度至朗、緯度不為空
if (latitudeStr != nil && latitudeStr.length > 0
&& longtitudeStr != nil && longtitudeStr.length > 0)
{
// 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域
[self locateToLatitude:latitudeStr.floatValue
longitude:longtitudeStr.floatValue];
}
}
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude
{
// 設(shè)置地圖中心的經(jīng)谷炸、緯度
CLLocationCoordinate2D center = {latitude , longitude};
// 設(shè)置地圖顯示的范圍,
MKCoordinateSpan span;
// 地圖顯示范圍越小幔睬,細(xì)節(jié)越清楚
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
// 創(chuàng)建MKCoordinateRegion對(duì)象背亥,該對(duì)象代表了地圖的顯示中心和顯示范圍般眉。
MKCoordinateRegion region = {center,span};
// 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍
[self.mapView setRegion:region animated:YES];
}