使用iPhone中的“地图视图"读取当前位置名称 [英] Read current location name using Map View in iPhone

查看:62
本文介绍了使用iPhone中的“地图视图"读取当前位置名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读取了当前位置的纬度和经度值,然后成功将该位置固定在iPhone中.现在,我想使用此经度和纬度值来读取该地名.

I read the current location's latitude and longitude values and then pin that location in iPhone successfully. Now I want to read that place name using this latitude and longitude values.

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

I used the following code to read the find the current location:

- (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协议:

Use the MKReverseGeocoderDelegate Protocol like this :

@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];
}

现在实现以下方法委托以获取响应:

Now implement the following method delegate to get the response :

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

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

For more informations look at this : http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/

这篇关于使用iPhone中的“地图视图"读取当前位置名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆