地图的使用及GPS定位

学习了地图及大头针的使用。发现还是蛮

简单的就能实现对地图的操作。

首先,我们要了解苹果的定位组件:

Wifi定位,通过查询一个Wifi路由器的地理位置的信息。比较省电,iPod touch和iPad也可以采用。

蜂窝基站定位,通过移动运用商基站定位。也适合有3G版本的iPod touch和iPad。

GPS卫星定位,通过3-4颗GPS定位位置定位,最为准确,但是耗电量大,不能遮挡。

他们的好坏都在上面,选择用哪种,就看你自己的需求了。

其次,就是了解定位框架。Core Location是iPhone、iPad等开发定位服务应用程序的框架。我们要在Xcode中添加“CoreLocation.framework”存在的框架。主要使用的类是:CLLocationManager,通过CLLocationManager实现定位服务CLLocationManagerDelegate是定位服务的委托。

常用的位置变化回调方法是:locationManager: didUpdateToLocation: fromLocation: locationManager: didFailWithError:该委托方法不仅可以获得当前位置(newLocation),还可以获得上次的位置(oldLocation ),CLLocation 对象coordinate.latitude属性获得经度,coordinate.longitude属性获得纬度。

这里有比较全面的属性介绍:

CLLocationCoordinate2D coordinate; //以经度和纬度表示的位置信息

CLLocationDistance altitude;  //海拔

CLLocationAccuracy horizontalAccuracy; //水平精度(如:精确到米)

CLLocationAccuracy verticalAccuracy; //垂直精度

CLLocationDirection course; //行驶方向

CLLocationSpeed speed; //速度

//经度和纬度

coordinate.latitude;//纬度

coordinate.longitude; //经度

然后,就是大家最关心的代码部分了。

新建一个大头针类:

.h

#import

@interfaceLocationObject:NSObject{

CLLocationCoordinate2Dcoordinate;

NSString*_titleString;//title值

NSString*_subTitleString;

float_latitude;//经度值

float_longitude;//纬度值

}

@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;

@propertyfloat_latitude;//经度值

@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函数

- (NSString*)title

{

returnself._titleString;

}

-(NSString*)subtitle

{

return_subTitleString;

}

@end

然后在你需要添加的mapView控制类里面去创建,添加大头针。

1、创建注解:

LocationObject*aLocationObject=[[LocationObjectalloc]initWithTitle:nameStringlatitue:[latitudeStringfloatValue]longitude:[longitudeStringfloatValue]];

aLocationObject._subTitleString=addressString;

2、添加注解:

先构建一个注解数组NSMutableArray*_mapAnnotations;

然后

[self._mapAnnotationsaddObject:aLocationObject];

[self._mapViewaddAnnotations:self._mapAnnotations];

3、删除注解:

删除注解可执行removeAnnotation:一次只删除一个注解,或者执行removeAnnotation:删除一个数组中的所有项。

如果需要使地图视图回到无注解状态,可执行:

[self._mapViewremoveAnnotations:self._mapView.annotations];

删除其中全部注解,MKMapView的annotations属性获取了所有注解的数组,然后从地图上全部删除。

这样就OK了!还可以为大头针进行自定义哟

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容