Mapkit中有没有什么函数可以一直跟踪当前可见区域?
Is there any function can keep tracking current visible region in Mapkit?
这是我为我的应用程序所做的,如您所见,顶部是 mapView
,下面是 TableView
。我想实现的是当我放大(移动到或缩小)到mapView
中的某个区域时,下面的tableView
可以呈现相应的用户信息。
为了实现该功能,这是我的解决方案:
我在地图上做了几个不可见的坐标,这些坐标就是包含用户信息的区域。
我想用mapView.visibleMapRect
获取当前可见区域,那么我想用MKMapRectContainsPoint(mapRect, invisibleCoordination)
检查当前可见区域是否包含某种不可见坐标,如果MKMapRectContainsPoint
returns true,那么我会进行web服务请求,然后在TableView
.
上呈现
但是,我不知道把这些功能放在哪里,如果我想解决这个问题,我必须继续跟踪mapView.visibleMapRect和MKMapRectContainsPoint
。我试过 locationManager(:didUpdateLocations)
,但是这个函数在 mapView
期间没有一直调用。
我不确定我的解决方案是否合适,如果您能告诉我更好的解决方案,我期待看到它。
MKMapViewDelegate 中有一个方法,每次更改区域时都会调用该方法。我想这就是你要找的。
你之前试过的方法,didUpdateLocations,在设备位置改变时调用。
MKMapViewDelegate
Swift:
optional func mapView(_ mapView: MKMapView!, regionDidChangeAnimated animated: Bool)
Objective-C:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
所以在你的情况下
Swift:
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!
}
}
Objective-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
这是我为我的应用程序所做的,如您所见,顶部是 mapView
,下面是 TableView
。我想实现的是当我放大(移动到或缩小)到mapView
中的某个区域时,下面的tableView
可以呈现相应的用户信息。
为了实现该功能,这是我的解决方案:
我在地图上做了几个不可见的坐标,这些坐标就是包含用户信息的区域。
我想用
mapView.visibleMapRect
获取当前可见区域,那么我想用MKMapRectContainsPoint(mapRect, invisibleCoordination)
检查当前可见区域是否包含某种不可见坐标,如果MKMapRectContainsPoint
returns true,那么我会进行web服务请求,然后在TableView
. 上呈现
但是,我不知道把这些功能放在哪里,如果我想解决这个问题,我必须继续跟踪mapView.visibleMapRect和MKMapRectContainsPoint
。我试过 locationManager(:didUpdateLocations)
,但是这个函数在 mapView
期间没有一直调用。
我不确定我的解决方案是否合适,如果您能告诉我更好的解决方案,我期待看到它。
MKMapViewDelegate 中有一个方法,每次更改区域时都会调用该方法。我想这就是你要找的。
你之前试过的方法,didUpdateLocations,在设备位置改变时调用。
MKMapViewDelegate
Swift:
optional func mapView(_ mapView: MKMapView!, regionDidChangeAnimated animated: Bool)
Objective-C:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
所以在你的情况下
Swift:
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!
}
}
Objective-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