gpt4 book ai didi

ios - 在 iPhone 中使用 map View 读取当前位置名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:34:45 26 4
gpt4 key购买 nike

我读取了当前位置的纬度和经度值,然后在 iPhone 中成功固定了该位置。现在我想使用这个纬度和经度值读取那个地名。

我使用以下代码来读取查找当前位置:

- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{

CLLocation *whereIAm = userLocation.location;

NSLog(@"I'm at %@", whereIAm.description);

latitude = whereIAm.coordinate.latitude;
longitude = whereIAm.coordinate.longitude;

NSLog(@"LAT : %f",latitude);
NSLog(@"LONG : %f",longitude);

mapView.mapType=MKMapTypeStandard;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude =latitude; //22.569722 ;
region.center.longitude = longitude; //88.369722;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];

[mapView setDelegate:self];

DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @" Welcome";
ann.subtitle = @"Hi";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}


- (void)viewDidLoad {
[super viewDidLoad];

mapView.showsUserLocation = YES;
mapView.delegate = self;
}



-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id<MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
}
[mapView.userLocation setTitle:@"I am here"];
return pinView;
}

最佳答案

像这样使用 MKReverseGeocoderDelegate 协议(protocol):

@interface YourViewController : UIViewController <MKReverseGeocoderDelegate> 
{
}


@implementation YourViewController

- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation *whereIAm = userLocation.location;
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:whereIAm.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}

现在执行以下方法委托(delegate)来获取响应:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSLog(@"I'm at %@", placemark.locality);
}

有关更多信息,请查看:http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/

关于ios - 在 iPhone 中使用 map View 读取当前位置名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3822297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com