iphone:MKMapView

來源:互聯網
上載者:User

在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];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.