Issue
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];
}
}
}
Solution
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:
- The inner loop doesn't seem to make any sense.
- Why don't you access the fields directly?
- myLat and myLon should be NSString object references.
Maybe something like this works better:
-(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];
if ( [fields count] != 3 ) {
// something is wrong/unexpected here, act appropriately
}
else {
NSString *mytitle = [fields objectAtIndex:0];
NSString *myLat = [fields objectAtIndex:1];
NSString *myLon = [fields objectAtIndex:2];
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];
}
}
}
Answered By - Volker Voecking
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.