要获取当前位置的信息,除了用CLLocation库之外,还可以用Mapkit,而我个人认为Mapkit在你需要得到更准确的位置信息时更有用,也十分方便.用法如下:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSLog(@"MKReverseGeocoder has failed.");
}
这个函数就不解释了,看名字你懂的~
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
currentCityLabel.text = placemark.locality;
}
这个函数是在geocoder获取了信息之后就会调用的,不需要手动调用
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
[self getCurrentCity];
// we have received our current location, so enable the "Get Current Address" button
}
这个是要在地图上显示当前的位置
-(void) getCurrentCity
{
self.reverseGeocoder =
[[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
NSLog(@"%g",mapView.userLocation.location.coordinate.latitude);
NSLog(@"%g",mapView.userLocation.location.coordinate.longitude);
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
初始化geocoder 用当前的经纬信息来初始化geocoder
在geocoder中会有很多参数,比如城市名,街道,等等很多信息。
[转Iphone]获取当前位置
最新推荐文章于 2024-05-21 10:43:23 发布