在iphone上實現地圖並不難。在Frameworks中加入MapKit.framework,要對mapView做相應的操作時要添加一個outlet,記得#import<MapKit/MapKit.h>即可。
可參考部落格 ios利用MKMapView實現簡單的地圖
顯示當前自己位置:利用MKMapView顯示自己當前位置的地圖
加入CoreLocation.framework,
VC遵循
<CLLocationManagerDelegate>
mapView.showsUserLocation=YES; CLLocationManager *locationManager = [[CLLocationManager alloc] init];//建立位置管理器 locationManager.delegate=self;//設定代理 locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度層級 locationManager.distanceFilter=1000.0f;//設定距離篩選器 [locationManager startUpdatingLocation];//啟動位置管理器 MKCoordinateSpan theSpan; //地圖的範圍 越小越精確 theSpan.latitudeDelta=0.05; theSpan.longitudeDelta=0.05; MKCoordinateRegion theRegion; theRegion.center=[[locationManager location] coordinate]; theRegion.span=theSpan; [mapView setRegion:theRegion]; [locationManager release];
在xcode中設定模擬器的位置,參照:
xcode4.2 模擬器定位 。xcode4.2 添加GPX檔案。手工指定位置。
給當前位置加圖釘:
使用的是MKMapViewDelegate的mapView:viewForAnnotation:方法
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { MKPinAnnotationView *pinView = nil; static NSString *defaultPinID = @"com.invasivecode.pin"; pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; pinView.pinColor = MKPinAnnotationColorRed; pinView.canShowCallout = YES; pinView.animatesDrop = YES; [mapView.userLocation setTitle:@"歐陸經典"]; [mapView.userLocation setSubtitle:@"vsp"]; return pinView; }
參照:http://www.cocoachina.com/iphonedev/sdk/2010/1020/2216.html
MKAnnotationView 有一個image屬性,應該可以改掉圖釘的圖形,改為flag?!
http://www.helmsmansoft.com/index.php/archives/980羅盤功能
//設定經緯度
CLLocationCoordinate2D coord = {39.904667,116.408198};
//設定顯示範圍
MKCoordinateSpan span = MKCoordinateSpanMake(0.4,0.4);
//設定地圖顯示的中心和範圍
MKCoordinateRegion region = MKCoordinateRegionMake(coord,span);
//根據設定的資訊進行顯示
[mapView setRegion region animated:NO];
[mapView sizeToFit];