循环访问数据库中的坐标数组时出现问题
我可以解析数据库中的数据,但很难循环遍历数据库中的所有坐标并将它们绘制在地图视图上。有人可以帮忙吗?
-(void)scanDatabase{
UIDevice *device = [UIDevice currentDevice];
NSString *uid = [device uniqueIdentifier];
NSString *myUrl = [NSString stringWithFormat:@"http://address.php?uid=%@",uid];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: myUrl ]];
// to receive the returend value
NSString *serverOutput =[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSArray *components = [serverOutput componentsSeparatedByString:@"\n"];
for (NSString *line in components) {
NSArray *fields = [line componentsSeparatedByString:@"\t"];
[eventPoints addObjectsFromArray:fields];
int count = [fields count];
for(int i=0;i < count; i++) {
int myindex0 = i*count;
int myindex1 = (i*count)+1;
int myindex2 = (i*count)+2;
NSString *mytitle = [eventPoints objectAtIndex:myindex0];
NSNumber *myLat = [eventPoints objectAtIndex:myindex1];
NSNumber *myLon = [eventPoints objectAtIndex:myindex2];
CLLocationCoordinate2D loc;
loc.latitude = myLat.doubleValue;
loc.longitude = myLon.doubleValue;
customAnnotation *event = [[customAnnotation alloc] initWithCoordinate:loc];
event.title = mytitle;
MKPinAnnotationView *newAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:event reuseIdentifier:@"simpleAnnotation"];
newAnnotationPin.pinColor = MKPinAnnotationColorRed;
[map addAnnotation:event];
}
}
}
I can parse data from the database but having a hard time looping through all the coordinates in the database and plotting them on the mapview. Can someone please help.
-(void)scanDatabase{
UIDevice *device = [UIDevice currentDevice];
NSString *uid = [device uniqueIdentifier];
NSString *myUrl = [NSString stringWithFormat:@"http://address.php?uid=%@",uid];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: myUrl ]];
// to receive the returend value
NSString *serverOutput =[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSArray *components = [serverOutput componentsSeparatedByString:@"\n"];
for (NSString *line in components) {
NSArray *fields = [line componentsSeparatedByString:@"\t"];
[eventPoints addObjectsFromArray:fields];
int count = [fields count];
for(int i=0;i < count; i++) {
int myindex0 = i*count;
int myindex1 = (i*count)+1;
int myindex2 = (i*count)+2;
NSString *mytitle = [eventPoints objectAtIndex:myindex0];
NSNumber *myLat = [eventPoints objectAtIndex:myindex1];
NSNumber *myLon = [eventPoints objectAtIndex:myindex2];
CLLocationCoordinate2D loc;
loc.latitude = myLat.doubleValue;
loc.longitude = myLon.doubleValue;
customAnnotation *event = [[customAnnotation alloc] initWithCoordinate:loc];
event.title = mytitle;
MKPinAnnotationView *newAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:event reuseIdentifier:@"simpleAnnotation"];
newAnnotationPin.pinColor = MKPinAnnotationColorRed;
[map addAnnotation:event];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用委托方法来添加注释视图,
否则它永远不会出现,请参阅此处
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html
you need to use the delegate method to add the annotation view
otherwise it will never appear see here
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html
不太确定您遇到了什么问题,因为“很难过......”并没有很好地描述它们:-)
看看您的代码,我喜欢做出以下评论:
也许这样的方法效果更好:
Not really sure what problems you are experiencing since "having a hard time..." doesn't describe them very well :-)
Looking at your code I like to make the following comments:
Maybe something like this works better: