學(xué)習(xí)了地圖及大頭針的使用。發(fā)現(xiàn)還是蠻
簡單的就能實(shí)現(xiàn)對(duì)地圖的操作。
首先,我們要了解蘋果的定位組件:
Wifi定位,通過查詢一個(gè)Wifi路由器的地理位置的信息。比較省電,iPod touch和iPad也可以采用。
蜂窩基站定位,通過移動(dòng)運(yùn)用商基站定位。也適合有3G版本的iPod touch和iPad。
GPS衛(wèi)星定位,通過3-4顆GPS定位位置定位,最為準(zhǔn)確,但是耗電量大,不能遮擋。
他們的好壞都在上面,選擇用哪種,就看你自己的需求了。
其次,就是了解定位框架。Core Location是iPhone、iPad等開發(fā)定位服務(wù)應(yīng)用程序的框架。我們要在Xcode中添加“CoreLocation.framework”存在的框架。主要使用的類是:CLLocationManager,通過CLLocationManager實(shí)現(xiàn)定位服務(wù)。CLLocationManagerDelegate是定位服務(wù)的委托。
常用的位置變化回調(diào)方法是:locationManager: didUpdateToLocation: fromLocation: locationManager: didFailWithError:該委托方法不僅可以獲得當(dāng)前位置(newLocation),還可以獲得上次的位置(oldLocation ),CLLocation 對(duì)象coordinate.latitude屬性獲得經(jīng)度,coordinate.longitude屬性獲得緯度。
這里有比較全面的屬性介紹:
CLLocationCoordinate2D coordinate; //以經(jīng)度和緯度表示的位置信息
CLLocationDistance altitude;??//海拔
CLLocationAccuracy horizontalAccuracy; //水平精度(如:精確到米)
CLLocationAccuracy verticalAccuracy; //垂直精度
CLLocationDirection course; //行駛方向
CLLocationSpeed speed; //速度
//經(jīng)度和緯度
coordinate.latitude;//緯度
coordinate.longitude; //經(jīng)度
然后,就是大家最關(guān)心的代碼部分了。
新建一個(gè)大頭針類:
.h
#import
@interfaceLocationObject:NSObject{
CLLocationCoordinate2Dcoordinate;
NSString*_titleString;//title值
NSString*_subTitleString;
float_latitude;//經(jīng)度值
float_longitude;//緯度值
}
@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;
@propertyfloat_latitude;//經(jīng)度值
@propertyfloat_longitude;//緯度值
@property(nonatomic,copy) NSString *_titleString;//title值
@property(nonatomic,copy) NSString *_subTitleString;
-(id)initWithTitle:(NSString*)atitle latitue:(float)alatitude longitude:(float)alongitude;
@end
---------------------
.m
@implementationLocationObject
@synthesizecoordinate,_latitude,_longitude,_titleString,_subTitleString;
-(id)initWithTitle:(NSString*)atitle latitue:(float)alatitude longitude:(float)alongitude
{
if(self=[superinit])
{
self._titleString=atitle;
self._latitude=alatitude;
self._longitude=alongitude;
}
returnself;
}
-(CLLocationCoordinate2D)coordinate;
{
CLLocationCoordinate2DcurrentCoordinate;
currentCoordinate.latitude=self._latitude;
currentCoordinate.longitude=self._longitude;
returncurrentCoordinate;
}
// 重寫title和subtitle的set函數(shù)
- (NSString*)title
{
returnself._titleString;
}
-(NSString*)subtitle
{
return_subTitleString;
}
@end
然后在你需要添加的mapView控制類里面去創(chuàng)建,添加大頭針。
1、創(chuàng)建注解:
LocationObject*aLocationObject=[[LocationObjectalloc]initWithTitle:nameStringlatitue:[latitudeStringfloatValue]longitude:[longitudeStringfloatValue]];
aLocationObject._subTitleString=addressString;
2、添加注解:
先構(gòu)建一個(gè)注解數(shù)組NSMutableArray*_mapAnnotations;
然后
[self._mapAnnotationsaddObject:aLocationObject];
[self._mapViewaddAnnotations:self._mapAnnotations];
3、刪除注解:
刪除注解可執(zhí)行removeAnnotation:一次只刪除一個(gè)注解,或者執(zhí)行removeAnnotation:刪除一個(gè)數(shù)組中的所有項(xiàng)。
如果需要使地圖視圖回到無注解狀態(tài),可執(zhí)行:
[self._mapViewremoveAnnotations:self._mapView.annotations];
刪除其中全部注解,MKMapView的annotations屬性獲取了所有注解的數(shù)組,然后從地圖上全部刪除。
這樣就OK了!還可以為大頭針進(jìn)行自定義喲