[英]Redrawing markers on a Route-me map
Working in iOS 6, I cannot seem to get the map view to update itself to show markers added or removed. 在iOS 6中工作,我似乎无法获取地图视图来更新自身以显示添加或删除的标记。
When the app opens, a number of markers are placed on the map. 当应用程序打开时,地图上会放置许多标记。 The user can then make choices that add new markers and/or remove existing markers. 然后,用户可以做出添加新标记和/或删除现有标记的选择。 This uses the same method as when the RMMarkerManager is populated at viewWillLoad, and I can iterate through the markers in the RMMarkerManager and see that it has the new set of markers, but the map view never updates to show them. 这使用与在ViewWillLoad处填充RMMarkerManager时相同的方法,并且我可以遍历RMMarkerManager中的标记并看到它具有新的标记集,但是地图视图从不更新以显示它们。
I have tried [mapview setNeedsDisplay] with no effect. 我尝试了[mapview setNeedsDisplay]无效。
Clearly, I'm missing something that causes the mapview to update the display of the markers, but I have not figured out what, despite lots of head-scratching and digging through documentations and posts. 显然,我遗漏了一些会导致mapview更新标记显示的内容,但是尽管有很多头文件和文档和帖子,但我仍未弄清楚。
I will appreciate any suggestions as to what I should change or add. 对于我应该更改或添加的内容,我将不胜感激。
As requested, here is the appropriate code. 根据要求,这里是适当的代码。 I'll explain how it works. 我会解释它是如何工作的。
In the viewController's createMarkers
method, the markers are created by accessing a sqlite database. 在viewController的createMarkers
方法中,通过访问sqlite数据库来创建标记。 One marker is created for each record that I want displayed as a marker on the map. 我要为每个要在地图上显示为标记的记录创建一个标记。 I then iterate through the array of markers, adding each to the mapView's marketManager ( addMarker
method). 然后,我遍历标记数组,将每个标记添加到mapView的addMarker
( addMarker
方法)。 The method createMarkers
is called in the viewController's viewWillLoad
method, and works correctly: all markers are displayed. 在视图控制器的viewWillLoad
方法中调用了createMarkers
方法,该方法可以正常工作:显示所有标记。
When using the app, a user can select or de-select records in the database. 使用该应用程序时,用户可以选择或取消选择数据库中的记录。 Then the viewController receives a notification that the user has changed the selection, and calls its setMarkers
method. 然后,viewController收到用户已更改选择的通知,并调用其setMarkers
方法。 The mapview's marketmanager gets a removeMarkers message, then the marker array is re-created; Mapview的市场经理收到removeMarkers消息,然后重新创建标记数组; it now has markers reflecting the user's selections. 现在,它具有反映用户选择的标记。 But the map never updates the on-view markers. 但是地图永远不会更新视图标记。 Markers removed by the user are not removed on the view; 用户删除的标记不会在视图上删除; markers added by the user are not added. 用户添加的标记不会添加。
After the update, I can iterate through mapview.markermanager.markers and see that it now contains the new markers. 更新之后,我可以遍历mapview.markermanager.markers并看到它现在包含新的标记。 But they're never shown on the mapView. 但是它们永远不会显示在mapView上。
Class: Marker, subclass of RMMarker. 类:Marker,RMMarker的子类。 Simply holds data about the marker to be displayed Marker.h : 只需保存有关要显示的标记的数据Marker.h :
// Marker.h
#import <Foundation/Foundation.h>
#import "Location.h"
#import "RMMarker.h"
@interface Marker : RMMarker {
NSString *category_name;
NSString *name;
NSString *desc;
NSString *address;
NSString *png;
int marker_id;
float lat;
float longi;
CLLocationCoordinate2D node;
float label_x_offset;
float label_y_offset;
}
@property (nonatomic, strong) NSString *category_name;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *desc;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *png;
@property (nonatomic) int marker_id;
@property (nonatomic) float lat;
@property (nonatomic) float longi;
@property (nonatomic) CLLocationCoordinate2D node;
@property (nonatomic) float label_x_offset;
@property (nonatomic) float label_y_offset;
@end
Class: Markers Holds an NSMutableArray of Markers, which is populated from a sqlite database: 类:标记保存一个NSMutableArray of Markers,该数组从sqlite数据库填充:
// Markers.m
#import "Markers.h"
#import "defs.h"
#import "FileLocator.h"
#import "Marker.h"
@implementation Markers
@synthesize markers;
-(NSMutableArray *) createMarkers {
markers = [[NSMutableArray alloc] init];
[self openDatabase];
NSString *query = [NSString stringWithFormat:@"SELECT categories.selected, categories.category_id, categories.png, places.name, address, description, latitude, longitude, place_id FROM places, categories WHERE (categories.selected = 1 AND places.category_id = categories.category_id);"];
debugPrintArgs(@"query: %@", query);
FMResultSet *rs = [db executeQuery:query];
while ([rs next]) {
Marker *marker = [[Marker alloc] init];
marker.marker_id = [rs intForColumn:@"place_id"];
marker.name = [rs stringForColumn:@"name"];
marker.address = [rs stringForColumn:@"address"];
marker.desc = [rs stringForColumn:@"description"];
marker.lat = [rs doubleForColumn:@"latitude"];
marker.longi = [rs doubleForColumn:@"longitude"];
marker.png = [rs stringForColumn:@"png"];
debugPrintArgs(@"%@, %@, %@, %f, %f", marker.name, marker.address, marker.description, marker.lat, marker.longi);
marker.label_y_offset = 150.0f;
marker.label_x_offset = 30.0f;
[markers addObject:marker];
}
[db close];
return markers;
}
@end
Methods in the viewcontroller: setMarkers: Iterates through the NSMUtableArray Markers, calling the method addMarker: for each marker in that array: viewcontroller中的方法:setMarkers:遍历NSMUtableArray Markers,为该数组中的每个标记调用addMarker:方法:
- (void) setMarkers {
// class Markers is essentially an NSMutableArray that holds instantiations of Marker - one for each marker to be displayed
// Markers is also responsible for populating itself from a sqlite database via the createMarkers method
Markers *markers = [[Markers alloc] init];
NSMutableArray *allMarkers = [markers createMarkers];
// allMarkers contains the markers to be displayed.
CLLocationCoordinate2D loc;
if ([allMarkers count] > 0) {
for (Marker *mrkr in allMarkers) {
loc.longitude = mrkr.longi;
loc.latitude = mrkr.lat ;
[self addMarker: mrkr at:loc withText:mrkr.name xOffset: mrkr.label_x_offset yOffset: mrkr.label_y_offset png: mrkr.png];
}
}
}
Also in the viewController: addMarker And, finally, the addMarker
method used to add a marker to the RMMarkerManager: 同样在viewController中:addMarker最后,使用addMarker
方法向addMarker
添加标记:
- (void) addMarker: (Marker *) marker at:(CLLocationCoordinate2D)loc withText:(NSString *)text xOffset: (float) x_offset yOffset:(float) y_offset png:(NSString *) png {
UIImage* markerImage = [UIImage imageNamed:png];
[marker replaceUIImage:markerImage anchorPoint:CGPointMake(0.38f, 1.08f)];
[viewMap.markerManager addMarker: marker AtLatLong: loc];
CGPoint position = CGPointMake( 0.0f, 0.0f);
[marker changeLabelUsingText: text position: position ];
}
The offending line here is in setMarkers
: 令人反感的行在setMarkers
:
Markers *markers = [[Markers alloc] init];
Making markers
an instance variable and doing the alloc-init in the class's viewWillLoad
method corrected the problem. 使markers
成为实例变量并在类的viewWillLoad
方法中执行alloc-init可以解决此问题。
I wish I could explain this better, but I am unsure as to why this caused the problem. 我希望我能更好地解释这一点,但是我不确定为什么会导致此问题。 But, it's corrected now and I'll explore when I've got some time. 但是,现在已更正,我将在有空的时候进行探讨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.