当用户位置更新或使用默认值时刷新 MKMapView
当设备获取用户位置时,我试图刷新地图视图并从服务器加载新数据。 这就是我正在做的:
- (void)viewDidLoad {
CGRect bounds = self.view.bounds;
mapView = [[MKMapView alloc] initWithFrame:bounds];
mapView.showsUserLocation=YES;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];
[self refreshMap];
}
- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//this is to avoid frequent updates, I just need one position and don't need to continuously track the user's location
if (userLocation.location.horizontalAccuracy > self.myUserLocation.location.horizontalAccuracy) {
self.myUserLocation = userLocation;
CLLocationCoordinate2D centerCoord = { userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude };
[theMapView setCenterCoordinate:centerCoord zoomLevel:10 animated:YES];
[self refreshMap];
}
}
- (void)refreshMap {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.mapView removeAnnotations:self.mapView.annotations];
//default position
NSString *lat = @"45.464161";
NSString *lon = @"9.190336";
if (self.myUserLocation != nil) {
lat = [[NSNumber numberWithDouble:myUserLocation.coordinate.latitude] stringValue];
lon = [[NSNumber numberWithDouble:myUserLocation.coordinate.longitude] stringValue];
}
NSString *url = [[NSString alloc] initWithFormat:@"http://myserver/geo_search.json?lat=%@&lon=%@", lat, lon];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[url release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
我还有一个 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data,用于创建和添加注释。
发生的情况如下: 有时我会在一段时间后获得位置,所以我已经加载了接近默认位置的数据。 然后我获取位置,它应该删除旧注释并添加新注释,但我总是看到旧注释并添加新注释(假设我每次收到 10 个注释,所以这意味着我的地图上应该有 10 个,但我却有 20 个)。
我做错了什么?
I'm trying to refresh my map view and load new data from the server when the device acquires the user location.
Here's what I'm doing:
- (void)viewDidLoad {
CGRect bounds = self.view.bounds;
mapView = [[MKMapView alloc] initWithFrame:bounds];
mapView.showsUserLocation=YES;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];
[self refreshMap];
}
- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//this is to avoid frequent updates, I just need one position and don't need to continuously track the user's location
if (userLocation.location.horizontalAccuracy > self.myUserLocation.location.horizontalAccuracy) {
self.myUserLocation = userLocation;
CLLocationCoordinate2D centerCoord = { userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude };
[theMapView setCenterCoordinate:centerCoord zoomLevel:10 animated:YES];
[self refreshMap];
}
}
- (void)refreshMap {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.mapView removeAnnotations:self.mapView.annotations];
//default position
NSString *lat = @"45.464161";
NSString *lon = @"9.190336";
if (self.myUserLocation != nil) {
lat = [[NSNumber numberWithDouble:myUserLocation.coordinate.latitude] stringValue];
lon = [[NSNumber numberWithDouble:myUserLocation.coordinate.longitude] stringValue];
}
NSString *url = [[NSString alloc] initWithFormat:@"http://myserver/geo_search.json?lat=%@&lon=%@", lat, lon];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[url release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I also have a - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data, to create and add the annotations.
Here's what happens:
sometimes I get the location after a while, so I already loaded data close to the default location.
Then I get the location and it should remove the old annotations and add the new ones, instead I keep seeing the old annotations and the new ones are added anyway (let's say I receive 10 annotations each time, so it means that I have 20 on my map when I should have 10).
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这可能有点长,但它对我有用,我真的希望它能对你有所帮助。
这是来自一个用于在驾驶员之间共享不同交通状态报告的应用程序。
我也遇到了这个问题,我尝试从服务器加载注释,然后删除它们,并在每次用户自己向服务器发送注释/按“刷新”按钮/每 1.5 分钟时将注释数组重新加载到地图 - 所以他将永远拥有当前的一套。
所以我认为这可能与从服务器加载新注释所需的时间间隔有关,或者与数组本身有关,后来我意识到它也可能与整个代码的组织方式有关,也许有些事情只是妨碍了其他人/时机。
我所做的基本上是将
[self.map addAnnotations:AN ARRAY OF ANNOTATIONS]
命令移动到主线程,并将整个加载过程移到后面,我还使用了一个临时数组而不是“self.map”。 map.annotations”删除当前注释,之后工作正常,所以我就这样留下了:),尽管我不是世界级专家(甚至不是世界级专家),我确信其他人可能有更专业的专家和有效的解决方案,代码示例:请对我的答案发表评论,因为我也很乐意学习一种不太复杂的方法来解决这个问题。
我认为如果有一种方法可以在使用后台线程时“等到完成”,那么大多数此类问题都可以得到解决,因为现在发生的情况是代码在下载-解析-创建的整个过程之前继续向前运行注释加载到地图的过程实际上已经完成。
Ok this might be a little long but it worked for me and I really do hope it will help you.
This is from an app that is used to share different traffic status reports between drivers.
I had this problem also, I tried to load annotations from a server, then delete them and reload the annotations array to the map every time the user sends an annotation to the server himself/presses the "refresh" button/every 1.5 minutes - so that he will always have the current set.
So I thought it might have something to do with the time interval that it takes to load the new annotations from the server, or with the array itself, later I realized it might also be connected to the way the entire code was organized and that maybe some things just got in the way/timing of others.
What I did is basically moving the
[self.map addAnnotations:AN ARRAY OF ANNOTATIONS]
command to the main thread and the whole loading process to the back, I also used a temp array instead of "self.map.annotations" to delete the current annotations, it worked ok afterwards so I just left it like that :), though I'm not a world-class expert (not even close) and I'm sure others might have a more professional and efficient solution, code sample:Please do comment on my answer since I will also be very happy to learn a less complicated way of solving this matter.
I think most of these type of problems would have been solved if there was a way to "wait until done" when using background threads, because now what happens is that the code continues to run forward before the whole process of downloading-parsing-creating annotation-loading to map process is actually finished.