【问题标题】:Is there any function can keep tracking current visible region in Mapkit?是否有任何功能可以跟踪 Mapkit 中的当前可见区域?
【发布时间】:2015-09-09 07:48:33
【问题描述】:

这是我为我的应用程序所做的,如您所见,顶部是mapView,下面是TableView。我想要实现的是,当我放大(移动到或缩小)mapView 中的某个区域时,下方的tableView 可以呈现相应的用户信息。 为了实现该功能,这是我的解决方案:

  1. 我在地图上做了几个不可见的坐标,这些坐标是包含用户信息的区域。

  2. 我想用mapView.visibleMapRect来获取当前可见区域,然后我想用MKMapRectContainsPoint(mapRect, invisibleCoordination)来检查当前可见区域是否包含某些不可见的坐标,如果MKMapRectContainsPoint返回true,那么我会发出网络服务请求,然后在TableView 上显示它。

但是,我不知道把这些函数放在哪里,如果我想解决这个问题,我必须不断跟踪mapView.visibleMapRect和MKMapRectContainsPoint。我尝试了locationManager(:didUpdateLocations),但在呈现mapView 期间,此函数不会继续调用。

我不确定我的解决方案是不是一个好的解决方案,如果你能告诉我一个更好的解决方案,我很期待看到它。

【问题讨论】:

    标签: ios swift mapkit cllocationmanager


    【解决方案1】:

    MKMapViewDelegate 中有一个方法,每次更改区域时都会调用该方法。我想这就是你要找的那个。

    您之前尝试的方法 didUpdateLocations 在设备位置更改时调用。

    MKMapViewDelegate

    斯威夫特:

    optional func mapView(_ mapView: MKMapView!, regionDidChangeAnimated animated: Bool)
    

    目标-C:

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    

    所以你的情况

    斯威夫特:

    import UIKit
    import MapKit
    
    class ViewController: UIViewController, MKMapViewDelegate
    {
        @IBOutlet weak var mapView: MKMapView! = nil
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            mapView.delegate = self
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool)
        {
            // Do what you want to do here!
        }
    }
    

    目标-C:

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    
    @interface ViewController : UIViewController < MKMapViewDelegate >
    
    @property (nonatomic, weak) IBOutlet MKMapView *mapView;
    
    @end
    
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.mapView.delegate = self;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    {
        // Do what you want to do here
    }
    
    @end
    

    【讨论】:

    • 您对如何实现此功能有更好的想法吗?
    • 如果你想做的是你上面写的我认为你是在正确的轨道上!! :)
    猜你喜欢
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多