標籤:des style blog class code c
兩個月前,抱著對iOS開發的興趣開始接觸蘋果裝置的開發,開始接觸object -C,以後用OC來代替,本人新人一枚,以後會經常寫一些東西給大家分享,有做的不到位的,請大家多多包涵。今天是我發表的第一篇部落格。有什麼錯誤的或者是寫的不到位的情況。請大家指正,大家共同進步。今天呢我來介紹一些關於iOS開發過程中的地圖類。
OC是物件導向的語言,和我們接觸的C++,JAVA是一樣的。在OC中除了基本的資料類型外,其他的基本上是通過對象來調用的。言歸正傳,iOS開發過程中,添加地圖首先用到的主要的類和資料結構有:MKMapView是用來顯示地圖的視圖,CLLocationCoordinate2D是用來表示一個地區的地理座標,在CLLocationCoordinate2D中需要注意的是 :東經和北緯表示是正座標。MKCoordinateRegion 是用來表示地圖的顯示地區,它的定義是
typedefstruct {
CLLocationCoordinate2D center;//用來表示位置的中心座標
MKCoordinateSpan span;//水平和垂直顯示地圖的大小,也定義了地圖的zoom層級
} MKCoordinateRegion;
MKMapView 中有很多屬性可以設定 比如:zoomEnabled scrollEnabled showsUserLocation 等方法,可以根據需求設定。更多可以查看蘋果的官方文檔。
self.mapView =[[MKMapViewalloc] initWithFrame:self.view.bounds];
self.mapView.mapType =MKMapTypeStandard;
self.mapView.zoomEnabled =YES ;
self.mapView.scrollEnabled =YES;
self.mapView.showsUserLocation =NO;
self.mapView.delegate =self;
CLLocationCoordinate2D的初始化有以下幾種:
1.CLLocationCoordinate2D
coordinate = { , };
2. CLLocationCoordinate2D coordinate;
coordinate.latitude =34.4668711;
coordinate.longitude =113.453221
// CLLocationCoordinate2D coordinate ={34.4668711,113.453221}; CLLocationCoordinate2D coordinate; coordinate.latitude =34.4668711;
coordinate.longitude =113.453221;
MKCoordinateSpan span = {1,1};//
表示顯示地區的精度,值越小表示的範圍越精確,值越大表示的範圍越大,但是不精確
下面是根據地理座標和span建立表示地區的值
MKCoordinateRegion的初始化方法也有如下幾種
1. MKCoordinateRegion region = {coordinate,span}; //初始化方法
2. MKCoordinateRegion region;
region = {coordinate,span};
region = MKCoordinateRegionMake(coordinate, span);
3. region = MKCoordinateRegionMakeWithDistance(coordinate, 111*1000*span.latitudeDelta, 111*1000*span.longitudeDelta); 中間傳的參數是已經定義好的。
MKCoordinateRegion region ={coordinate,coordinateSpan};// // MKCoordinateRegion region ;// region =MKCoordinateRegionMake(coordinate, coordinateSpan);// region =MKCoordinateRegionMakeWithDistance(coordinate, coordinateSpan.latitudeDelta, coordinateSpan.longitudeDelta);// // [self.mapView setRegion:region];
CLLocationManager 使用來管理位置資訊的。蘋果官方給出的文檔如下:
The CLLocationManager class defines the interface for configuring the delivery of location- and heading-related events to your application. You use an instance of this class to establish the parameters that determine when location and heading events should be delivered and to start and stop the actual delivery of those events. You can also use a location manager object to retrieve the most recent location and heading data.
self.locationManager =[[CLLocationManager alloc] init]; self.locationManager.delegate =self; self.locationManager.distanceFilter =kCLDistanceFilterNone; self.locationManager.desiredAccuracy =kCLLocationAccuracyBest; [self.locationManager startUpdatingLocation];
有了上述的方法就可以簡單的實現一個地圖的功能了。
我們再來看看一些使用到的方法,我只是提一些比較常用的並且主要的方法。
在CLLocationManager中擷取位置資訊的方法
1.- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation //這個方法在6.0以後就取消了
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// CLLocationDistance distance =[newLocation distanceFromLocation:oldLocation];
// NSLog(@"dis is %f",distance/1000.0);
}
2。- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
這兩個方法都可以擷取位置資訊,但是如果兩個方法都寫得話 只會執行第二個方法。 所以推薦使用地二個方法。
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
這個方法可以擷取程式執行過程中得錯誤資訊,沒有的話就位空。
有了上述方法我們就可以建立一個簡單的顯示地圖的程式了。
當然了,地圖上面還有一個比較重要的類就是Annotation類,這個類提供地圖的註解。那麼如何在地圖上實現一個Annotation呢
我們來看看annotation。MKAnnotation中有協議和委託(如果沒有實現系統會預設產生一個紅色圖釘的註解表徵圖)
實現註解類的基本步驟有:
*建立註解類(需要聲明實現MKAnnotation協議)
*執行個體化註解類對象
*將執行個體化的註解類對象添加到MKMapView對象
*類似於UITableViewCell的方法建立MKAnnotationView 對象,這個對象用於最終在地圖上面顯示。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
//當在mapView上添加annotation對象的時候,這個方法會被調用,用來設定顯示介面;
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } static NSString *indetifier =@"Indentifier"; QYCustomView *pinAnnotationView =(QYCustomView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indetifier]; if (nil ==pinAnnotationView) { pinAnnotationView =[[QYCustomView alloc] initWithAnnotation:annotation reuseIdentifier:indetifier]; } pinAnnotationView.canShowCallout =YES; UIImageView *leftImagView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; leftImagView.image =[UIImage imageNamed:@"Icon 2"]; pinAnnotationView.leftCalloutAccessoryView =leftImagView; UIButton *btn =[UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [btn addTarget:self action:@selector(onButton:) forControlEvents:UIControlEventTouchUpInside]; pinAnnotationView.rightCalloutAccessoryView =btn; return pinAnnotationView; }
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;//這個方法是當MkAnnotationView 對象被點擊的時候調用
當然了,這些方法是代理方法,在開始的時候應該實現它的delegate 。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Tapped" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView show];}
下面來介紹實現一個圖釘的方法的介紹
MKPointAnnotation *pointAnnotation =[[MKPointAnnotationalloc] init]; pointAnnotation.coordinate =coordinate; pointAnnotation.title =@"河南鄭州"; pointAnnotation.subtitle =@"河南青雲";
在這裡需要注意的是MkpointAnnotation 相當於一個Model ,只是用來提供資料的,如果只是有這個資訊是不會顯示的,它是依附於MKAnnotationView 的對象的,添加圖釘從天而降的方法是添加一個 MKAnnotationView對象 pinAnnotationView pinAnnotationView.animatesDrop =YES;就行了。
下面我們來講一下位置的反編碼,具體的理解應該是知道CLLocation 對象的資訊,如何擷取具體的國家,省份,省會,街道的具體的資訊。具體的反編碼要用到CLGeocoder對象,還有一個就是CLPlacemark 類,CLPlacemark反編碼後的位置結果,包含位置資訊國家,省,區(市),街道,位置名等。
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { if (nil != error) { NSLog(@"error info is %@",error); return
} // CLPlacemark for (CLPlacemark *placeMark in placemarks) {
NSLog(@"%@,%@,%@,%@",placeMark.country,placeMark.administrativeArea,placeMark.locality,placeMark.thoroughfare); }
}];
想要列印什麼資訊看有什麼需求,具體的請查看蘋果官方文檔。還有中間用到了block塊的概念,如果有什麼不懂,請參考block塊的資訊。block塊的使用就是代碼中以^開頭的。
有了上述的資訊大家基本上可以完成常用的功能實現,但是現在有位置資訊的app,像高德地圖,嘀嘀打車等app 顯示的內容比較豐富,最後我們就來將一下怎麼樣自訂自己的視圖。
首先自訂一個自己的類繼承自 MKAnnotationView,如:QYAnnotationView : MKAnnotationView。然後再這個類中重寫
- (void)setSelected:(BOOL)selected這個方法,當然也可以不定義自己的類。
- (void)setSelected:(BOOL)selected {
[super setSelected:selected]; UIView *view = [[UILabel alloc] initWithFrame:CGRectMake(-40, -40, 100, 40)] view.backgroundColor = [UIColororangeColor]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; imageView.image = [UIImage imageNamed:@"icon"]; [view addSubview:imageView]; [self addSubview:view]; }
還有一種方法就是重寫
- (void)setSelected:(BOOL)selected animated:(BOOL)animated這個方法,產生的效果是一樣的,只不過後面的這個添加了一個動畫。有興趣的可以分別寫兩個看看區別是什麼,這裡就不在多說了
但是這樣就完成了嗎 ?大家會發現,當點擊表徵圖的時候出現兩個重疊的視圖,不是自己想要的結果怎麼辦?
那麼如何解決這個問題呢 ,其實很簡單 ,就是在在定義MKAnnotationView 的對象上面修改它的canShowCallout = NO;親測可用,希望能幫到被這個問題困擾的人。