示例中心
功能在线体验
menu 导航 closed 关闭

标注点支持3DTouch效果

该示例展示了如何让地图上的标注点支持3DTouch,预览标注点信息。
00:00 / 01:00
扫描二维码在手机端体验功能
体验移动端 扫码体验移动端

使用场景

如果希望利用iPhone的3DTouch功能,对地图上的标注点进行信息预览、快捷操作,利用该示例说明可以实现。

用到产品

iOS 地图 SDK

核心类/接口

接口

说明

版本

AMapSearchAPI

- (void)AMapPOIAroundSearch:(AMapPOIAroundSearchRequest *)request;

POI 周边查询接口

V4.0.0版本起

核心难点

1.检查是否支持3D Touch功能


- (void)check3DTouch
{
    // register for 3D Touch (if available)
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        [self registerForPreviewingWithDelegate:(id)self sourceView:self.view];
        NSLog(@"3D Touch is available! Hurra!");
    }
    else
    {
        NSLog(@"3D Touch is not available on this device. Sniff!");
    }
}

2.实现3D Touch delegate


- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    __block BOOL touchOnAnnotation = NO;
    __block AMapPOI *poi = nil;

    [self.annotationArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[MAPinAnnotationView class]])
        {
            MAPinAnnotationView *view = (MAPinAnnotationView *)obj;
            CGPoint point = [view convertPoint:location fromView:self.mapView];

            if (CGRectContainsPoint(view.bounds, point))
            {
                touchOnAnnotation = YES;

                if ([view.annotation isKindOfClass:[POIAnnotation class]])
                {
                    POIAnnotation *annotion = (POIAnnotation *)view.annotation;
                    poi = annotion.poi;
                }

                *stop = YES;
            }
        }
    }];

    if (!touchOnAnnotation)
    {
        return nil;
    }

    // check if we're not already displaying a preview controller
    if ([self.presentedViewController isKindOfClass:[POIPreviewViewController class]])
    {
        return nil;
    }
    // shallow press: return the preview controller here (peek)
    self.selectedPoi = poi;

    POIPreviewViewController *previewController = [[POIPreviewViewController alloc] initWithUserPoint:self.userLocation.coordinate selectedPOI:self.selectedPoi];
    previewController.delegate = self;
    previewController.preferredContentSize = CGSizeMake(0, 400);

    return previewController;
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{

    // deep press: bring up the commit view controller (pop)
    UIViewController *commitController = [[POIPreviewViewController alloc] initWithUserPoint:self.userLocation.coordinate selectedPOI:self.selectedPoi];

    [self showViewController:commitController sender:self];
}
//是否支持3D Touch,如果支持,注册一下
func check3DTouch() {
    if #available(iOS 9.0, *) {
        if self.traitCollection.forceTouchCapability == UIForceTouchCapability.available {
            self.registerForPreviewing(with: self, sourceView: self.view)
            print("3D Touch is available! Hurra!");
        }
    } else {
        print("3D Touch is not available on this device. Sniff!");
    }
}

//3DTouch刚触发的时候弹出的模态视图
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    var touchOnAnnotation = false
    var selectPoi: AMapPOI?

    //找到点击的哪个点
    for (_, poiAnnotationView) in self.poiAnnotationViews.enumerated() {

        if poiAnnotationView.frame.contains(location) {

            touchOnAnnotation = true;

            let poiAnno: POIAnnotation = poiAnnotationView.annotation as! POIAnnotation

            selectPoi = poiAnno.poi

            break;
        }

    }

    //没有点击在任何一个Annotation的范围内
    if touchOnAnnotation == false {
        return nil
    }

    // check if we're not already displaying a preview controller
    if self.presentedViewController != nil {
        return nil
    }

    self.selectedPOI = selectPoi

    let poiDetailVC = self.createPOIDetailVC()
    poiDetailVC.isFrom3DTouchPresent = true
    poiDetailVC.preferredContentSize = CGSize.init(width: 0, height: 400)

    return poiDetailVC

}

//模态视图弹出后,继续按住屏幕不放,就会调用下面这句话,进入VC
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {

    let poiDetailVC = self.createPOIDetailVC()
    self.show(poiDetailVC, sender: self)

}


//Preview Actions
@available(iOS 9.0, *)
lazy var previewActions: [UIPreviewActionItem] = {

    func previewActionForTitle(_ title: String, index: NSInteger, style: UIPreviewActionStyle = .default) -> UIPreviewAction {

        return UIPreviewAction(title: title, style: style) { previewAction, viewController in
            self.showRoute(index:index)
        }

    }

    let action1 = previewActionForTitle("步行",index:1)
    let action2 = previewActionForTitle("公交",index:2)
    let action3 = previewActionForTitle("驾车",index:3)

    return [action1, action2, action3]

}()

@available(iOS 9.0, *)
override var previewActionItems: [UIPreviewActionItem] {
    return previewActions
}