0

我正在为 unity3d 创建一个插件以使用区域监控。但是,CllocationManager 事件并未启动。经过大量搜索后,我读到这个 ​​CLLocationManager 需要在主循环中初始化。但是,当我使用插件时,我不确定该怎么做。

插件代码: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

区域监控.mm

#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];
        }
    }
}

统一代码:

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;

    }
} 

我统一呼唤的地方:

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");     }

           } 
4

1 回答 1

0

我可能是你在那个线程上没有运行循环。因此,如果您的代码在后台线程上运行,则必须在那里运行循环。

更多信息: https ://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

检查配置运行循环的部分。

于 2018-11-05T21:39:25.613 回答