移动设备的定位功能很强大,可以做很多有意思的应用,比如LBS,GPS等等。
Core Location依赖于3种不同的方式,每种方式在给定的平台上可能可用,也可能不可用。这些方式受每种设备内置功能的限制。
1.Wi-Fi定位扫描本地路由 器,使用它们的MAC地址搜索一个中心位置数据库,所有iPhone和iPod touch平台都免费提供了这种定位方式。
2.蜂窝定位则依赖于天线,但是只有iPhone上才有这样的天线。这种技术利用本地蜂窝基站进行三角定位,这些基站的位置是在手机公司安装基站时就确 定的。最后一种、也是最准确的一种定位方式是GPS,只有第二代和更新的iPhone才提供GPS。第一代iPhone没有内置GPS,目前所有iPod touch设备也没有提供GPS。
3.第三代iPhone 3G S引入了一个内置的罗盘(通过一个磁强计),并提供了用于支持它的Core Location API。
下面说一下如何在程序中使用
首先,把CoreGraphics.framework 加入到framework
其次,声明中要加入<CLLocationManagerDelegate>协议
然后,创建一个位置管理器CLLocationManager *locationManager;
self.locationManager= [[CLLocationManageralloc]init];//初始化
locationManager.delegate=self;//委托自己
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//精度设定,有好几种选择(kCLLocationAccuracyNearestTenMeters,kCLLocationAccuracyHundredMeters,kCLLocationAccuracyKilometer,kCLLocationAccuracyThreeKilometers精度逐渐降低)
[locationManagerstartUpdatingLocation];//开启位置更新
//重写两个位置服务器委托方法
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation {
if(startingPoint==nil)
self.startingPoint= newLocation;
newLocation.coordinate.latitude;//经度
newLocation.coordinate.longitude;//纬度
newLocation.horizontalAccuracy;//水平精度
newLocation.altitude;//高度
newLocation.verticalAccuracy;//垂直精度
CLLocationDistancedistance = [newLocation
getDistanceFrom:startingPoint];
NSString*distanceString = [[NSStringalloc]
initWithFormat:@”%gm”, distance];//格式化移动距离
distanceTraveledLabel.text= distanceString;
[distanceStringrelease];
}
- (void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error {
NSString*errorType = (error.code ==kCLErrorDenied) ?
@”Access Denied”:@”Unknown Error”;
UIAlertView*alert = [[UIAlertViewalloc]
initWithTitle:@”Error getting Location”
message:errorType
delegate:nil
cancelButtonTitle:@”Okay”
otherButtonTitles:nil];
[alertshow];
[alertrelease];
}
下次说一下如何利用已经得到的经纬度获取地区和城市名