当前位置:编程学习 > wap >>

iPhone获取当前位置信息

要获取当前位置的信息,除了用CLLocation库之外,还可以用Mapkit,而我个人认为Mapkit在你需要得到更准确的位置信息时更有用,也十分方便.用法如下:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoderdidFailWithError:(NSError *)error
{
   NSLog(@"MKReverseGeocoder has failed.");
}
这个函数就不解释了,看名字你懂的~
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoderdidFindPlacemark:(MKPlacemark *)placemark
{
   currentCityLabel.text = placemark.locality;
}
//用CLLoction获取到当前的经纬度
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)newLocationfromLocation:(CLLocation *)oldLocation

{
  
   CLLocationDistance l = newLocation.coordinate.latitude;//得到经度
  
   CLLocationDistance v =newLocation.coordinate.longitude;//得到纬度
  
  
  
    NSLog(@"%f%f", l,v);
  
  
  
    CLLocation*new = [[CLLocation alloc] initWithLatitude: 11.0 longitude:12.0];
  
  
  
   CLLocationDistance u = [newLocation distanceFromLocation:new];
  
  
  
    NSLog(@"%f",u);
  
  
  
    [selfstartedReverseGeoderWithLatitude: l longitude: v];
  
}

 

- (void)startedReverseGeoderWithLatitude:(double)latitudelongitude:(double)longitude{
  
   CLLocationCoordinate2D coordinate2D;
  
   coordinate2D.longitude = longitude;
  
   coordinate2D.latitude = latitude;
  
   MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]initWithCoordinate:coordinate2D];
  
   geoCoder.delegate = self;
  
    [geoCoderstart];
  
}

#pragma mark -

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoderdidFindPlacemark:(MKPlacemark *)placemark

{
   NSLog(@"当前城市:%@",placemark.locality);

  
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoderdidFailWithError:(NSError *)error

{
  
}

初始化geocoder 用当前的经纬信息来初始化geocoder
在geocoder中会有很多参数,比如城市名,街道,等等很多信息.


摘自 进阶码农的专栏
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,