【问题标题】:Drop pin at current user location iphone mkmapview在当前用户位置放置 pin iphone mkmapview
【发布时间】:2012-07-24 04:16:46
【问题描述】:

好的,这是我使用 CLLoactionManager 的尝试

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    //mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];


    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;

    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    mStoreLocationButton.hidden=FALSE;
    location=newLocation.coordinate;
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.01;
    span.longitudeDelta=0.01;
    region.span=span;

    [mapView setRegion:region animated:TRUE];    
}

我的问题是[locationManager startUpdatingLocation]; 似乎没有触发下一个方法。我错过了什么?我试过在第二种方法中设置断点,但它们永远不会捕获。显然它没有被使用。

【问题讨论】:

  • 如果您不想要 pin,那么您可以使用 [yourMapView setShowsUserLocation:YES];

标签: ios mkmapview coordinates


【解决方案1】:

您应该考虑使用 CLLocationManager 来获取当前位置,以便为您的 MKMapView 提供正确的坐标。

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
MKMapView *map = [[MKMapView alloc] init];

[locationManager startUpdatingLocation];

CLLocationCoordinate2D _coordinate = locationManager.location.coordinate;
MKCoordinateRegion extentsRegion = MKCoordinateRegionMakeWithDistance(_coordinate, 800, 800); 

[map setRegion:extentsRegion animated:YES];

【讨论】:

  • 我需要创建一个新类来实现这个吗?或者这会出现在我上面的代码中吗?
  • 此代码只是 .m 实现。显然你也需要头文件。我将编写一个更详尽的示例,并在第二天左右发布。
【解决方案2】:

This 看起来是个不错的起点。为了放下大头针,您需要知道坐标。使用- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 将帮助您获取用户的位置,以便您可以创建MKAnnotationMKPinAnnotationViews。一旦开始,它就非常简单。

【讨论】:

  • 我尝试按照指南进行操作,但initWithCoordinate:coordinate andTitle:_title andSubtitle:_title 给我一个错误,即没有声明此选择器的@interface。
  • 啊,是的..我忘记了注释代码..谢谢你指出这一点。我将把它包含在我的修订示例中,很快就会发布。
猜你喜欢
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 2015-03-27
  • 2011-08-11
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 2014-08-29
相关资源
最近更新 更多