首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用用户方向旋转MKmap

使用用户方向旋转MKmap
EN

Stack Overflow用户
提问于 2013-01-31 23:01:11
回答 1查看 2.9K关注 0票数 1

我到处看了看,但在这个问题上几乎找不到什么。我正在尝试实现一个“找到你的车”功能到我正在创建的应用程序中。

我有一个地图视图,用户可以用来存储他们的汽车的位置。然后,他们可以点击“查找汽车”,在mapView上显示他们的当前位置和汽车。(见下文)

我的问题是:如何让地图旋转以显示用户面对的方向?这样我就可以告诉用户他们需要走哪条路。与原生地图ap非常相似。

从我所做的阅读来看,我相信这将在didUpdateToLocation: delegate方法中完成,但我不太确定。

(只要意识到我的用户在湖中,但这不是问题所在)

这是代码,我已经用来添加引脚和适合他们在屏幕上。

CODE

编辑:

我使用fguchelaar在评论中提供的链接,设法让它正常工作。我更改了代码,使其如下所示。这段代码允许地图旋转,但有一些奇怪的行为。请参阅HERE

代码语言:javascript
复制
- (IBAction)BTNPinCar:(id)sender {


    CLLocation *location = [locationManager location];
    // Configure the new event with information from the location

    float longitude=location.coordinate.longitude;
    float latitude=location.coordinate.latitude;

    NSLog(@"dLongitude : %f", longitude);
    NSLog(@"dLatitude : %f", latitude);

    [self pins:latitude lon:longitude];

    parked.latitude = latitude;
    parked.longitude = longitude;

    //set the reigion to the coords and a mile distance
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(parked, startZoom , startZoom );
    //change the region and animate it
    [MapView setRegion:viewRegion animated:YES];



}

- (IBAction)BTNFindCar:(id)sender {

    MKMapRect zoomRect = MKMapRectNull;
    for (id <MKAnnotation> annotation in MapView.annotations)
    {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
        if (MKMapRectIsNull(zoomRect)) {
            zoomRect = pointRect;
        } else {
            zoomRect = MKMapRectUnion(zoomRect, pointRect);
        }
    }
    [MapView setVisibleMapRect:zoomRect animated:YES];\

    // Start heading updates.
    if ([CLLocationManager headingAvailable]) {
        locationManager.headingFilter = 5;
        [locationManager startUpdatingHeading];
    }


}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
   // [MapView setTransform:CGAffineTransformMakeRotation(-1 * newHeading.magneticHeading * M_PI / 180)];
    double rotation = newHeading.magneticHeading * 3.14159 / 180;
    CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin

    [MapView setTransform:CGAffineTransformMakeRotation(-rotation)];

    [[MapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        MKAnnotationView * view = [MapView viewForAnnotation:obj];

        [view setTransform:CGAffineTransformMakeRotation(rotation)];
        [view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];

    }];

} 

编辑2:

解决了旋转问题;

变化

代码语言:javascript
复制
[MapView setTransform:CGAffineTransformMakeRotation(-rotation)];

代码语言:javascript
复制
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
EN

回答 1

Stack Overflow用户

发布于 2013-02-01 00:20:04

让贴图旋转的答案如下所示。

首先启动位置管理器并设置代理。

代码语言:javascript
复制
// locationManager update as location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;


// Start location services to get the true heading.
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];

上面的代码开始跟踪用户的位置。然后,您需要开始跟踪用户标题以旋转地图

代码语言:javascript
复制
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
    locationManager.headingFilter = 5;
    [locationManager startUpdatingHeading];
}

之后,实现LocationManager委托方法。

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;

最后,在该委托方法中设置跟踪模式。

代码语言:javascript
复制
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES]; 

希望这篇文章能帮助任何有同样问题的人

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14628764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档