手机定位 coco2d开发


手机定位 需要实现代理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];

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值