【发布时间】:2016-08-01 16:46:55
【问题描述】:
我们可以使用“发布通知”进行位置更新以在谷歌地图上绘制当前位置吗?或者除此之外还有什么更好的实现方式?虽然我不想在 googlemaps 中将 KVO 用于 @"Mylocations"。
在 LocationTracker.m 中
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for(int i=0;i<locations.count;i++)
{
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
[PlacesDetails sharedInstance].theLocation=theLocation;
if(newLocation != nil && (!(theLocation.latitude == 0.0 && theLocation.longitude == 0.0)))
{
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
// Below implemented the post notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLocation" object:nil];
}
}
}
在 ViewController.m 中
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateGMSCameraPostition) name:@"updateLocation"
object:nil];
}
-(void)updateGMSCameraPostition
{
NSLog(@"CALLED UPDATELOCATION OBSERVER");
mapView_.camera = [GMSCameraPosition cameraWithTarget:[PlacesDetails sharedInstance].theLocation
zoom:14];}
【问题讨论】:
-
这是一种合法的数据传递方式。如果您想查找它,则称为观察者模式。
-
@AMAN77 我使用了 KVO 模式,但崩溃了,控制台说“GMSMapView 已解除分配,而键值观察者仍向其注册”我实现了与链接“stackoverflow.com/questions/27193946/…”中引用的相同,并且从那里尝试了所有解决方案。所以我决定不使用 KVO。
标签: ios objective-c cllocationmanager nsnotificationcenter gmsmapview