启动 StartUpdatingLocation(( 后,每次更新位置时运行另一个方法。
我试图在public LocationManager ()
中调用MethodToRun()
方法
public LocationManager()
{
this.locMgr = new CLLocationManager
{
PausesLocationUpdatesAutomatically = false
};
locMgr.AllowsBackgroundLocationUpdates = true;
locMgr.DesiredAccuracy = 1;
locMgr.DistanceFilter = 1;
locMgr.StartUpdatingLocation();
MethodToRun();
}
async public void MethodToRun(){
// here I send the data to the server
// code ...
}
但数据只发送到服务器一次。
public LocationManager()
{
this.locMgr = new CLLocationManager
{
PausesLocationUpdatesAutomatically = false
};
locMgr.AllowsBackgroundLocationUpdates = true;
locMgr.DesiredAccuracy = 1;
locMgr.DistanceFilter = 1;
locMgr.StartUpdatingLocation();
}
async public void MethodToRun(){
// here I send the data to the server
// code ...
}
我需要在每次更新位置数据时发送数据。
实现一个ICLLocationManagerDelegate
实例并将其分配给您创建的CLLocationManager
实例。
ICLLocationManagerDelegate
中是来自位置管理器的回调,称为 UpdatedLocation
(ObjC: locationManager:didUpdateToLocation:fromLocation:
(。
ICLLocationManagerDelegate
添加到现有类或创建一个新类并实现UpdatedLocation
:
public class ALocationDelegate : NSObject, ICLLocationManagerDelegate
{
[Export("locationManager:didUpdateToLocation:fromLocation:")]
public void UpdatedLocation(CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation)
{
// do something with the updated location (newLocation)
}
}
现在,在 CLLocationManager 实例上,分配该委托的现有实例(或创建新实例(:
locMgr.Delegate = new ALocationDelegate();
回复:https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate