【发布时间】:2023-03-21 23:50:02
【问题描述】:
我在使用 iOS 和 gefencing 时遇到了一些问题...
//
// ViewController.m
//
//
// Created by me on 14.05.13.
// Copyright (c) 2013 me. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLRegion *testRegion;
}
#define METERS_PER_MILE 1609.344
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initlocationManager];
// [self startlocationManager];
}
// init location Manager Object, set delegation, result and accurate
- (void)initlocationManager {
//create location manager object
locationManager = [[CLLocationManager alloc]init];
//this instance send its messages to our AppDelegate
[locationManager setDelegate:self];
//to get all results from the location manager
[locationManager setDistanceFilter:kCLDistanceFilterNone];
//be accurate as possible
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
- (void)viewWillAppear:(BOOL)animated {
CLLocationCoordinate2D startLocation;
startLocation.latitude = +52.53753000;
startLocation.longitude= +13.35971000;
MKPointAnnotation *newLocation = [[MKPointAnnotation alloc] init];
newLocation.coordinate = startLocation;
newLocation.title = @"great title";
newLocation.subtitle = @"great subtitle";
MKCoordinateRegion startingRegion = MKCoordinateRegionMakeWithDistance(startLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
testRegion = [[CLRegion alloc] initCircularRegionWithCenter:startLocation radius:500 identifier:@"TEST"];
[_mapView addAnnotation:newLocation];
[_mapView setRegion:startingRegion animated:YES];
[locationManager startMonitoringForRegion:testRegion];
NSLog(@"%@",locationManager.monitoredRegions);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Error : %@",error);
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Entered Region - %@", region.identifier);
// [self showRegionAlert:@"Entering Region" forRegion:testRegion.identifier];
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Started monitoring %@ region", region.identifier);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
终端给我以下消息:
2013-05-15 00:24:47.739 [8712:13d03] {(
(identifier TEST) <+52.53753000,+13.35972000> radius 500.00m
)}
2013-05-15 00:24:47.972 [8712:13d03] Started monitoring TEST region
所以区域创建成功,监控也开始了
但从未调用过didEnterRegion 函数。
帮助会很棒!
编辑:
目前我在 iphone 模拟器中使用该 gpx 文件,它工作正常。只有didEnterRegion 永远不会被调用。
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx version="1.1" creator="http://www.geoplaner.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<wpt lat="52.53856" lon="13.3515">
<ele>36.6</ele>
<name>WP04-D</name>
</wpt>
<wpt lat="52.53753" lon="13.35972">
<ele>35.1</ele>
<name>WP05-E</name>
</wpt>
<wpt lat="52.538" lon="13.35788">
<ele>34.1</ele>
<name>WP06-F</name>
</wpt>
<wpt lat="52.53844" lon="13.35633">
<ele>33.6</ele>
<name>WP07-G</name>
</wpt>
<wpt lat="52.53895" lon="13.35392">
<ele>34.7</ele>
<name>WP08-H</name>
</wpt>
<wpt lat="52.53813" lon="13.34925">
<ele>36</ele>
<name>WP09-I</name>
</wpt>
<wpt lat="52.53794" lon="13.34667">
<ele>36.3</ele>
<name>WP10-J</name>
</wpt>
<wpt lat="52.53815" lon="13.34504">
<ele>37.1</ele>
<name>WP11-K</name>
</wpt>
<wpt lat="52.5369" lon="13.35938">
<ele>39.3</ele>
<name>WP12-L</name>
</wpt>
<wpt lat="52.53619" lon="13.35792">
<ele>33.3</ele>
<name>WP13-M</name>
</wpt>
<wpt lat="52.53468" lon="13.35508">
<ele>37.3</ele>
<name>WP14-N</name>
</wpt>
<wpt lat="52.53398" lon="13.35367">
<ele>37</ele>
<name>WP15-O</name>
</wpt>
<wpt lat="52.53781" lon="13.35862">
<ele>33.7</ele>
<name>WP16-P</name>
</wpt>
</gpx>
【问题讨论】:
-
你现在在那个位置吗?
-
我正在使用带有 gpx 文件的模拟器。我编辑我的第一篇文章。这样你就可以看到我的 gpx 文件和 gps 坐标了。
-
是否调用了其他任何 CLLocationManagerDelegate 方法?
-
我更新了我的答案;你的代码对我来说很好,但有两个变化。
-
@Hendrik Hi 遇到了和你一样的问题。如果测试手机它不工作,并用模拟器测试它工作。你如何解决问题
标签: ios locationmanager geofencing clregion