- Home /
CLLocationManager didstartregionMonitoring not being called
I am creating a plugin for unity3d to use region monitoring. However the CllocationManager events are not not firing up. After searching a lot I read that this CLLocationManager needs to initialized in the main loop. However I am not sure how to do that when I am using the plugin.
Plugin code: RegionMonitoring.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface RegionMonitoringPlugin : NSObject <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
-(void)enterRegionNotify;
-(void)leaveRegionNotify;
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)raduis;
@end
#import "RegionMonitoringPlugin.h"
@implementation RegionMonitoringPlugin
- (id) init
{
if (self = [super init]){
if (locationManager==nil){
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
}
return self;
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self enterRegionNotify];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self leaveRegionNotify];
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)regionwithError:(NSError *)error
{
NSLog(@"Location error %@, %@", error, @"Fill in the reason here");
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog (@"Started monitoring");
}
-(void)leaveRegionNotify
{
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= @"Region Left"; // ToAsk: What should be displayed
note.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
}
-(void)enterRegionNotify
{
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= @"Region Left"; //ToAsk: what should be displayed ?
note.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
}
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
{
[self leaveRegionNotify];
CLLocationCoordinate2D home;
home.latitude = latitude;
home.longitude = longitude;
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"region"];
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
[region release];
}
@end
extern "C" {
static RegionMonitoringPlugin *regionMonitor;
// Unity callable function to start region monitoring
BOOL _startRegionMonitoring(float m_latitude,float m_longitude, float m_radius)
{
if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled]){
NSLog (@"Region Monitoring not configured");
return NO;
}
if (regionMonitor == nil){
regionMonitor = [[RegionMonitoringPlugin alloc] init] ;
}
[regionMonitor startMonitor:m_latitude longitude:m_longitude radius:m_radius];
return YES;
}
void _showNotification (char *msg){
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Region Monitor Notification" message:[NSString stringWithUTF8String:(char *)msg] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
}
Unity Code:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class RegionMonitoringMediator {
/*Interface to native implementation */
[DllImport ("__Internal")]
private static extern bool _startRegionMonitoring (float m_latitude,float m_longitude, float m_radius);
[DllImport ("__Internal")]
private static extern void _showNotification(string mes);
public static void showNotification(string message){
if (Application.platform != RuntimePlatform.OSXEditor)
_showNotification(message);
}
public static bool startRegionMonitoring (float latitude,float longitude, float radius)
{
/*Call plugin only when running on real device*/
if (Application.platform != RuntimePlatform.OSXEditor)
return _startRegionMonitoring ( latitude , longitude , radius);
else return false;
}
}
Place where I am calling in unity:
private void callFourSquare (string queryURL)
{
WWW www = new WWW(queryURL);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
Debug.Log(WWW.UnEscapeURL (www.text));
bool started = RegionMonitoringMediator.startRegionMonitoring((28.023234f,77.232134F,10.0f);
if (!started)
Debug.LogError( "Could not start region monitoring"); }
}
Comment