【问题标题】:How to show my current position and region onto my MKMapView with using blue dots ?如何使用蓝点在我的 MKMapView 上显示我当前的位置和区域?
【发布时间】:2014-05-09 08:31:06
【问题描述】:

我正在尝试为 iOS 和情节提要创建虚拟地图视图演示,以允许获取当前用户位置。在执行方面,我必须滚动和滑动才能找到我的当前位置,但不能设置在我当前位置附近的区域。例如,我应该使用 MK Coordinates 或 MKMap Region 来实现什么?下面是我的 .m 实现代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_mapView setCenterCoordinate:_mapView.userLocation.location.coordinate animated:YES];
    MKAnnotationView *userLocationView = [_mapView viewForAnnotation:_mapView.userLocation];
    [userLocationView.superview bringSubviewToFront:userLocationView];
}

【问题讨论】:

  • 最简单的选项是将 userTrackingMode 设置为 MKUserTrackingModeFollow。否则,您可以在 didUpdateUserLocation 委托方法中设置地图的中心坐标(不需要 CLLocationManager)。不要尝试直接操作注释视图层次结构(使用bringSubviewToFront 等)。

标签: ios mkmapview


【解决方案1】:

MAPKIT

首先让您的地图显示您当前的位置。

 _mapView.showsUserLocation   =   YES; // This will show the blue dot on map at your current location.

现在设置区域以放大您当前的位置。

MKCoordinateRegion region;
    region.center.latitude           =      _mapView.userLocation.coordinate.latitude;;
    region.center.longitude          =      _mapView.userLocation.coordinate.longitude;;

    region.span.latitudeDelta        =      0.001;
    region.span.longitudeDelta       =      0.001;

    MKCoordinateRegion scaledRegion  =       [_mapView regionThatFits:region];
    [_mapView setRegion:scaledRegion animated:NO];

确保在您的位置在地图上可见或地图完全加载后设置区域。你可以利用委托..

- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView;
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;

如果您想自己管理 UserLocation.. 您可以使用CLLocationManager 类初始化它并使用它的委托来获取用户当前位置的更新。

CLLocationManager *locationManager  =           [[CLLocationManager alloc]init];
locationManager.desiredAccuracy     =           kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;
[locationManager setDelegate:self];

// 委托

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Use New Location to show your custom marker or do any thing you want.
}

为了在模拟器中进行调试,您可以设置 GPS 位置 转到模拟器选项 DEBUG-->LOCATION .. 您可以添加自定义位置或从可用位置中进行选择。

希望这会对您有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2013-11-13
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多