手机定位 coco2d开发(不同版本不同方法)

来源:互联网 发布:mac的qq可以远程吗 编辑:程序博客网 时间:2024/05/22 08:15

手机定位 需要实现代理CLLocationManagerDelegate,MKReverseGeocoderDelegate

并导入CoreLocation 框架。

代理使用方法 如下:

#pragma mark – 获取城市名称

// iso 5.0 以下版本使用此方法

- (void)startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{

CLLocationCoordinate2D coordinate2D;

coordinate2D.longitude = longitude;

coordinate2D.latitude = latitude;

MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D];

self.reverseGeocoder = geoCoder;

[geoCoder release];

self.reverseGeocoder.delegate = self;

[self.reverseGeocoder start];

}

#pragma mark –

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark

{

NSString *thoroughfare=placemark.thoroughfare;

if (thoroughfare) {

self.locationLabel.text = thoroughfare;

}

}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error

{

NSLog(@”获取失败”);

}

// IOS 5.0 及以上版本使用此方法

- (void)locationAddressWithLocation:(CLLocation *)locationGps

{

CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];

self.geoCoder = clGeoCoder;

[clGeoCoder release];

[self.geoCoder reverseGeocodeLocation:locationGps completionHandler:^(NSArray *placemarks, NSError *error)

{

NSLog(@"error %@ placemarks count %d",error.localizedDescription,placemarks.count);

for (CLPlacemark *placeMark in placemarks)

{

NSLog(@"地址:%@",placeMark.locality);

NSLog(@"地址:%@",placeMark.thoroughfare);

NSLog(@"地址:%@",placeMark.subLocality);

self.locationLabel.text = placeMark.thoroughfare;

}

}];

}

#pragma mark – location Delegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

NSLog(@”定位出错”);

}

- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

fromLocation:(CLLocation *)oldLocation

{

if (!newLocation) {

[self locationManager:manager didFailWithError:(NSError *)NULL];

return;

}

if (signbit(newLocation.horizontalAccuracy)) {

[self locationManager:manager didFailWithError:(NSError *)NULL];

return;

}

[manager stopUpdatingLocation];

NSLog(@”%f,%f”,newLocation.coordinate.latitude,newLocation.coordinate.longitude);

_coordinate.latitude = newLocation.coordinate.latitude;

_coordinate.longitude = newLocation.coordinate.longitude;

self.gpsLabel.text = [NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];

//解析并获取当前坐标对应得地址信息

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {

[self locationAddressWithLocation:newLocation];

}else {

[self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude

longitude:newLocation.coordinate.longitude];

}

}


转自:http://redstar.easymorse.com/?p=349

原创粉丝点击