当用户到达某个位置时如何触发视频?
Posted
技术标签:
【中文标题】当用户到达某个位置时如何触发视频?【英文标题】:How to trigger a video when a user reaches a certain location? 【发布时间】:2011-08-09 11:28:07 【问题描述】:这是我的 CoreLocationController.h 目标 c 类
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
extern const CLLocationAccuracy kCLLocationAccuracyBest;
@protocol CoreLocationControllerDelegate
@required
- (BOOL)startRegionMonitoring;
- (void)locationUpdate:(CLLocation *)location; // Our location updates are sent here
- (void)locationError:(NSError *)error; // Any errors are sent here
@end
@interface CoreLocationController : NSObject <CLLocationManagerDelegate, MKMapViewDelegate>
CLLocationManager *locMgr;
CLLocationCoordinate2D coordinate;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
id delegate;
@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, assign) id delegate;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@结束
这是 CorelocationController.m
#import "CoreLocationController.h"
@implementation CoreLocationController
@synthesize locMgr, delegate, coordinate;
- (id)init
self = [super init];
if(self != nil)
self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
self.locMgr.delegate = self; // Set the delegate as self.
[locMgr setDistanceFilter:kCLDistanceFilterNone];
[locMgr setDesiredAccuracy:kCLLocationAccuracyBest];
[worldView setShowsUserLocation:YES];
return self;
- (BOOL)startRegionMonitoring
if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled] )
return NO;
CLLocationCoordinate2D home;
home.latitude = +51.49410630;
home.longitude = -0.10251360;
CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:10.0 identifier:@"home"];
if (locMgr == nil)
locMgr = ([[CLLocationManager alloc] init]);
[locMgr startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
[region release];
return YES;
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
NSLog(@"Enteed location");
// [self leavingHomeNotify];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location entered" message:@"Region entered" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL];
[alert show];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) // Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
[self.delegate locationUpdate:newLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) // Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
[self.delegate locationError:error];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location failed" message:@"Location failed" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL];
[alert show];
- (void)dealloc
[self.locMgr release];
[super dealloc];
@end
这是 CoreLocationDemoViewer.h
#import <UIKit/UIKit.h>
#import "CoreLocationController.h"
#import <MapKit/MapKit.h>
@interface CoreLocationDemoViewController : UIViewController <CoreLocationControllerDelegate, MKMapViewDelegate>
CoreLocationController *CLController;
IBOutlet UILabel *locLabel;
MKMapView *worldView;
@property (nonatomic, retain) CoreLocationController *CLController;
@property (nonatomic, retain) IBOutlet UILabel *locLabel;
@property (nonatomic, retain) MKMapView *worldView;
@end
这是 CoreLocationDemoViewer.m
#import "CoreLocationDemoViewController.h"
@implementation CoreLocationDemoViewController
@synthesize CLController;
@synthesize locLabel;
@synthesize worldView;
- (void)viewDidLoad
[super viewDidLoad];
self.worldView.mapType = MKMapTypeStandard; // also MKMapTypeSatellite or MKMapTypeHybrid
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
worldView.zoomEnabled = YES;
worldView.scrollEnabled = YES;
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)u
CLLocationCoordinate2D loc = [u coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];
- (void)locationUpdate:(CLLocation *)location
locLabel.text = [location description];
// [mapView setCenterCoordinate:location.coordinate];
// [mapView setShowsUserLocation:YES];
CLLocationCoordinate2D coord = [location coordinate];
// Add it to the map view
// [worldView addAnnotation:mp];
// MKMapView retains its annotations, we can release
// [mp release];
// Zoom the region to this location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 50, 50);
[worldView setRegion:region animated:YES];
// [locationManager stopUpdatingLocation];
- (void)locationError:(NSError *)error
locLabel.text = [error description];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization.
return self;
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
*/
- (void)viewDidUnload
[super viewDidUnload];
self.worldView = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[CLController release];
[worldView release];
[super dealloc];
@end
好的,这就是我想要的。到目前为止,它显示了带有用户位置的地图 它会找到标签上显示的确切位置..如果我做错了什么,请纠正..
我想做的是在用户到达某个位置时触发视频...
我快到了吧?
【问题讨论】:
看起来您已经为位置片段设置了正确的一切。到目前为止,您的代码是否存在问题,或者只是想验证您迄今为止所做的工作? 【参考方案1】:我认为,如果您只是无法正确缩放 mapView,请使用这篇文章中的 MKCoordinate 内容,How do I zoom an MKMapView。
我正在开发一个地图视图,它可以缩放到合适的水平以供查看,我正在为地图设置 lattitudeDelta 和 longitudeDelta。对我有用。
【讨论】:
【参考方案2】:也许设置委托有问题?我通常像这样在 .h 文件中指定委托:
id <CoreLocationControllerDelegate> delegate;
和
@property (nonatomic, assign) id <CoreLocationControllerDelegate> delegate;
编辑
另外,您是否检查过-mapView:didUpdateUserLocation:
是否被调用?
【讨论】:
我刚刚插入了一个 NSLog 以查看是否调用了 mapview 并且似乎没有出现.. 我该怎么办??? NSLog(@"mapview 被调用");【参考方案3】:我没有看到您为 worldView 分配代理(但您为其他事情分配了代理)。尝试在您的viewDidLoad
中添加worldView.delegate = self;
。
【讨论】:
我做到了,但你认为 mapview 没有被调用没有区别吗?我能做些什么呢?非常感谢你hhhhhh我是一个新手 尝试在你认为没有被调用的地方添加一个断点。如果断点被命中,那么它就会被调用。如果没有,则不会调用该方法。只需单击编辑器左侧的行添加断点 (useyourloaf.com/blog/2011/2/21/xcode-breakpoint-actions.html)。以上是关于当用户到达某个位置时如何触发视频?的主要内容,如果未能解决你的问题,请参考以下文章
Xcode MapKit:如何在特定位置输入时触发事件[关闭]