iphone 如何使用地圖MapKit

來源:互聯網
上載者:User

                                                           iphone  如何使用地圖MapKit 

 

1。  首先在framework中加入 MapKit.framework

 

2。 類中   #import <MapKit/MapKit.h>

 

3。  類中定義  

       CLLocationManager *  locationManager;

       CLLocationCoordinate2D         curLocation;

 

       MKMapView  *    UsermapView;

 

4。類實現  CLLocationManagerDelegate,MKMapViewDelegate 兩個delegate

 

 

5。用上一篇中的方法,獲得了 使用者的所在的位置  curLocation。

 

    (1) 在擷取到當前的位置後,顯示當前位置所在位置的1000米所在地區。

 

      //響應當前位置的更新,在這裡記錄最新的當前位置<br />- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation<br />fromLocation:(CLLocation *)oldLocation<br />{<br />NSLog(@"newLocation:%@",[newLocation description]);</p><p>//如果是第一次載入地圖<br />if (UsermapView==nil)<br />{<br />UsermapView = [[MKMapView alloc] initWithFrame:self.view.bounds];<br />UsermapView.mapType = MKMapTypeStandard;<br />UsermapView.zoomEnabled=YES;<br />UsermapView.showsUserLocation=YES;<br />UsermapView.delegate=self;<br />[UsermapView autorelease];<br /> [self.view addSubview:UsermapView];<br /> }</p><p>//設定顯示地區<br />MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(newLocation.coordinate,1000 ,1000 );<br />[UsermapView setRegion:region animated:TRUE];<br />}<br /> 

 

      如果要顯示經緯度表示的(0.05,0.05)範圍:

      將上面的 地區設定為下面的代碼即可:

      //設定顯示地區<br />MKCoordinateSpan span=MKCoordinateSpanMake(0.05,0.05);<br />MKCoordinateRegion region=MKCoordinateRegionMake(newLocation.coordinate,span); 

 

      (2)  如何在地圖上增加地圖註解

             2。1  首先要從 MKAnnotationView 派生出一個自訂的 類,用來實現個人化的 註解。

            @interface MapAnnotation : NSObject <MKAnnotation><br />{<br />CLLocationCoordinate2D coordinate;<br />NSString * title;<br />NSString * subtitle;</p><p> //自己定義的其他資訊成員<br />}<br />@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;<br />@property (nonatomic,retain) NSString * title;<br />@property (nonatomic,retain) NSString * subtitle;<br />@end 

 

            @implementation MapAnnotation<br />@synthesize coordinate;<br />@synthesize title;<br />@synthesize subtitle;</p><p>- (id) initWithCoordinate:(CLLocationCoordinate2D) temp_coordinate<br />{<br /> if ([super init])<br /> {<br /> coordinate=temp_coordinate;<br /> }<br />return self;<br />}<br />- (void) dealloc<br />{<br />self.title=nil;<br />self.subtitle=nil;<br />[super dealloc];<br />}<br />@end<br /> 

 

            2。2 怎麼添加一個註解:

             //標註使用者<br /> CLLocationCoordinate2D userLocation;<br /> CLLocationDegrees latitude=111.005;<br /> CLLocationDegrees longtitude=21.015;<br /> userLocation.latitude=latitude;<br /> userLocation.longitude=longtitude;</p><p> MapAnnotation * annotation=[[[MapAnnotation alloc] initWithCoordinate:userLocation] autorelease];</p><p>annotation.title=@"小A";<br />annotation.subtitle=@"20歲";<br />[UsermapView addAnnotation:annotation];</p><p> 

 

             根據 註解 ,產生註解視圖

             - (void) mapView:(MKMapView *)mapView   didAddAnnotationViews:(NSArray*) views

             當增加一個註解的時候,上面的函數就會被調用,以產生一個註解視圖,下面的代碼是產生一個小人表徵圖,放到地圖上。

             //delegate MKMapViewDelegate<br />- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation<br />{<br />//判斷是否是自己<br /> if ([annotation isKindOfClass:[MapAnnotation class]]==YES)<br />{<br />MKAnnotationView * view;<br />view=(MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotation.title];</p><p>if (view==nil)<br />{<br />view=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];<br />}<br />else<br />{<br />view.annotation=annotation;<br />}</p><p>//設定表徵圖<br />MapAnnotation * Mapannotation=annotation;<br />[view setImage:[UIImage imageNamed:@"avatar_boy.png"] ];</p><p>view.canShowCallout=TRUE;<br />return view;<br />}<br />else<br />{<br />MapAnnotation * Mapannotation=annotation;<br />Mapannotation.title=@"當前位置";</p><p>return nil;<br />}<br />} 

 

             當使用者點擊地圖上面的註解視圖的時候,也就是點擊小人的時候,會調用下面的函數:

             - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

             

             //當使用者點擊小人表徵圖的時候,就進入這裡,即將顯示 AnnotationView<br />- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view<br />{</p><p>if ([view.annotation isKindOfClass:[MapAnnotation class]]==NO)<br />{<br />return ;<br />}</p><p> //設定顯示的視圖的內容<br /> MapAnnotation * annotation=(MapAnnotation *) view.annotation;<br /> //通過MapAnnotation就可以獲得自己設定的一些個人化資訊。<br /> //然後,根據這些資訊來設定,這裡是獲得頭像的檔案路徑,然後<br /> //設定到 VIEW<br /> UIImageView * headImageView= ( UIImageView *) view.leftCalloutAccessoryView ;<br />[headImageView setImage:[UIImage imageWithContentsOfFile: nsFilePath] ];<br />} 

 

 

             設定VIEW的左右邊分別是什麼內容,下面是設定左邊為一個Image,右邊是一個箭頭的button。

 

             - (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray*) views<br />{<br />int i=0;<br />for (MKPinAnnotationView *mkview in views )<br />{<br />//判斷是否是自己<br />if ([mkview.annotation isKindOfClass:[MapAnnotation class]]==NO)<br />{<br />continue;<br />}<br />else<br />{<br /> UIImageView * headImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"online.png"] ];<br /> [headImageView setFrame:CGRectMake(1, 1, 30, 32)];<br /> [headImageView autorelease];<br /> mkview.leftCalloutAccessoryView=headImageView;</p><p> UIButton * rightbutton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];<br /> mkview.rightCalloutAccessoryView=rightbutton;<br />}<br />i++;<br />}</p><p>}<br /> 

 

             當使用者點擊VIEW後,將會調用下面的函數:

             -(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *) control

 

             -(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *) control<br />{<br />MapAnnotation * annotation= view.annotation;</p><p> // 根據 MapAnnotation,取出個人化的個人資訊,然後建立自己<br /> // 的新的VIEW,並且顯示。<br />} 

 

   (3)  如何檢測到地圖顯示範圍的改變

          - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

 

          //當地圖顯示範圍發生變化的時候<br />- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated<br />{<br /> MKCoordinateRegion curRegin=UsermapView.region;</p><p>}     

 

 (4)  如何計算 當前顯示範圍的  顯示半徑

 

           MKCoordinateRegion curRegin=UsermapView.region;<br />MKMapPoint mapPointCenter=MKMapPointForCoordinate(curRegin.center);<br />CLLocationCoordinate2D outerCoordinate=curRegin.center;<br />outerCoordinate.latitude+= curRegin.span.latitudeDelta/2.0;<br />outerCoordinate.longitude+=curRegin.span.longitudeDelta/2.0;<br />MKMapPoint mapPointOuter=MKMapPointForCoordinate(outerCoordinate);</p><p>CLLocationDistance double_distance= MKMetersBetweenMapPoints(mapPointCenter,mapPointOuter);<br />unsigned int cur_distance=(unsigned int) double_distance;<br />NSLog(@"cur_distance=%d",cur_distance);<br /> 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.