要获取当前位置的信息,除了用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]获取当前位置
最新推荐文章于 2026-01-02 08:59:06 发布
本文介绍了如何利用Mapkit库来获取设备的地理位置信息,并通过示例代码详细展示了如何实现逆地理编码,将经纬度坐标转换为具体的城市名等位置信息。
1457





