annotations - NSArray - How to do this? - objective-c

I would like to annotations in a Mapview, using data fetched Json via a URL.
As might do?
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://example.com/LData.php"]];
//Data: Longitud/Latitud/Country;.....
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(str);
[foo removeAllObjects];
NSArray *foo2 =[str componentsSeparatedByString: #";"];
int i=0;
for(i=0;i<[foo2 count]; i++){
[foo insertObject:[foo2 objectAtIndex:i] atIndex:i];
NSArray *foo3 =[foo2 componentsSeparatedByString: #"/"];
So far so good
¿As I Lay to introduce the variables [i]?
CLLocationCoordinate2D theCoordinate[i];
theCoordinate1.latitude[i] = [foo3 objectAtIndex:1]);//Longitud
theCoordinate1.longitude[i] = [foo3 objectAtIndex:2]);//Latitud
MyAnnotation* myAnnotation[i]=[[MyAnnotation alloc] init];
myAnnotation[i].coordinate=theCoordinate[i];
myAnnotation[i].title=#""+[foo3 objectAtIndex:3];
myAnnotation[i].subtitle=#"in the city";
[mapView addAnnotation:myAnnotation[i]];
[annotations addObject:myAnnotation[i]];
}
how I can solve this problem?

Why you are using different CLLocationCoordinate2D variables?
theCoordinate1.latitude[i] = [foo3 objectAtIndex:1]);//Longitud
theCoordinate1.longitude[i] = [foo3 objectAtIndex:2]);//Latitud
and then
myAnnotation[i].coordinate=theCoordinate[i];
I think you need to adjust you code and some problems can resolved by themselves.

NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://example.com/LData.php"]];
//Data: Longitud/Latitud/Country;.....
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(str);
[foo removeAllObjects];
NSArray *foo2 =[str componentsSeparatedByString: #";"];
int i=0;
for(i=0;i<[foo2 count]; i++){
[foo insertObject:[foo2 objectAtIndex:i] atIndex:i];
NSArray *foo3 =[foo2 componentsSeparatedByString: #"/"];
float realLatitude = [[foo3 objectAtIndex:1] floatValue];//Longitud
float realLongitude = [[foo3 objectAtIndex:0] floatValue];//Latitud
MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [foo3 objectAtIndex:3];
//myAnnotation.subtitle = [note objectForKey:#"stationAddressKey"];
[_mapView setDelegate:self];
[_mapView addAnnotation:myAnnotation];
[myAnnotation release];
}
-(MKAnnotationView *)_mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MyAnnotation class]])
{
static NSString *reuseId = #"customAnn";
MKAnnotationView *customAnnotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (customAnnotationView == nil)
{
customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
UIImage *pinImage = [UIImage imageNamed:#"pin-green.png"];
[customAnnotationView setImage:pinImage];
customAnnotationView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customAnnotationView.rightCalloutAccessoryView = rightButton;
}
// NSString *iconFilename = #"";
// MyAnnotation *myAnn = (MyAnnotation *)annotation;
// if ([myAnn.stationIdKey isEqualToString:#"BP"])
iconFilename = #"bp-logo.png";
// else
// if ([myAnn.stationIdKey isEqualToString:#"Caltex"])
// iconFilename = #"caltex.png";
// else
// if ([myAnn.stationIdKey isEqualToString:#"Shell"])
// iconFilename = #"shell.png";
// UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease];
customAnnotationView.leftCalloutAccessoryView = leftIconView;
customAnnotationView.annotation = annotation;
return customAnnotationView;
}
return nil;
}
-(void)_mapView:(MKMapView *)_mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{ if ([view.annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *myAnn = (MyAnnotation *)view.annotation;
NSLog(#"callout button tapped for station id %#", myAnn.stationIdKey);
}
else
{
NSLog(#"callout button tapped for annotation %#", view.annotation);
} }
Is correct this?

Related

Make lazy loading faster and responsive in iOS App

In my iOS app I've a class that performs a web request to get an array of informations located in mySQL DB. In this class I've a method that do this taking as input a mySQL query:
- (NSMutableArray *) myreq:(NSString *)query{
// Create NSData object
NSData *dataQuery = [query
dataUsingEncoding:NSUTF8StringEncoding];
// Get NSString from NSData object in Base64
NSString *base64EncodedQuery = [dataQuery base64EncodedStringWithOptions:0];
// Print the Base64 encoded string
NSLog(#"Encoded: %#", base64EncodedQuery);
NSMutableString *strURL = [NSMutableString stringWithFormat:#"http://…=%#“,base64EncodedQuery];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:dataURL
options:kNilOptions
error:&error];
NSMutableArray *results = [[NSMutableArray alloc] init];
int numRow = 0;
for (NSArray *arrow in json) {
[results addObjectsFromArray:arrow];
numRow++;
}
return results;
}
This method send a query to a php script that perform immediately this query to MySQL DB and get a json with results. I translate the json in this method and finally return an array with results.
I call myreq in a method
- (void)downloadScope{
_arrID = [[NSMutableArray alloc] init];
_arrIDUsers = [[NSMutableArray alloc] init];
_arrUsernames = [[NSMutableArray alloc] init];
_arrPictures = [[NSMutableArray alloc] init];
[myQueue addOperation:[NSBlockOperation blockOperationWithBlock: ^{
query = #"SELECT ID FROM mytable”;
[_arrID addObjectsFromArray:[self myreq:query]];
for (int i = 0; i < [_arrID count]; i++) {
NSArray *tempArray = [[NSArray alloc] initWithArray:[self myreq:[NSString stringWithFormat:#"SELECT IDUsr,usrn, pictureaddress FROM mytable WHERE ID = %#",_arrID[i]]]];
[_arrIDUsers insertObject:tempArray[0] atIndex:i];
[_arrUsernames insertObject:tempArray[2] atIndex:i];
[_arrPictures insertObject:tempArray[2] atIndex:i];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.tableView reloadData];
}];
}
}]];
[myQueue setSuspended:NO];
}
In tableView I create cells in this way (using SDWebImage):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Identificatore di cella
NSString *identifier = #“cellmy”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.backgroundColor = nil;
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSString *username = [self.arrUsernames objectAtIndex:indexPath.row];
UILabel *cellLabelUsername = (UILabel *)[cell viewWithTag:2];
cellLabelUsername.text = [username uppercaseString];
UIImageView *cellImageProfileSnap = (UIImageView *)[cell viewWithTag:5];
[cellImageProfileSnap sd_setImageWithURL:[NSURL URLWithString:[_arrPictures objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:#“…”]];
}
In viewDidLoad I initialize my NSOperationQueue (defined in my interface):
myQueue = [[NSOperationQueue alloc] init];
[myQueue setMaxConcurrentOperationCount:100];
[myQueue setName:#"com.sada"];
My goal is to make everything faster because the loading in tableView is slow and I think that is not dependent on SDWebImage. Please help me

Objective-C / ALAssetslibrary - How can i change my method to work synchronously

I have implemented a method that retrieve correctly the informations about images using the Alassetslibrary. As you know, it works asynchronously but in my case i need that it works synchronously. I have only to read the informations about images and return to the method. My application as only one view that doesn't need a refresh. How can i modify my method? At the moment the line "return xmlList;" result always null, but i know that the assets are correctly read in the block because i have used a NSlog.
-(NSString *) getPhotos{
NSMutableArray *idList = [[NSMutableArray alloc] init];
NSMutableArray *widthList = [[NSMutableArray alloc] init];
NSMutableArray *heightList = [[NSMutableArray alloc] init];
NSMutableArray *orientationList = [[NSMutableArray alloc] init];
NSMutableArray *dateList = [[NSMutableArray alloc] init];
__block XMLWriter* xmlWriter = [[XMLWriter alloc]init];
__block NSString *xmlList;
__block NSString *test;
__block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSString *description = [[NSString alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSString *description = [asset description];
NSRange first = [description rangeOfString:#"URLs:"];
NSRange second = [description rangeOfString:#"?id="];
NSString *path = [description substringWithRange: NSMakeRange(first.location + first.length, second.location - (first.location + first.length))];
[idList addObject:path];
NSDictionary *data = [[asset defaultRepresentation] metadata];
NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:#"PixelWidth"];
NSString *widthString = [NSString stringWithFormat:#"%#", width];
[widthList addObject:widthString];
NSNumber *height = [[[asset defaultRepresentation] metadata] objectForKey:#"PixelHeight"];
NSString *heightString = [NSString stringWithFormat:#"%#", height];
[heightList addObject:heightString];
NSNumber *orientation = [[[asset defaultRepresentation] metadata] objectForKey:#"Orientation"];
NSString *orientationString = [NSString stringWithFormat:#"%#", orientation];
if(orientationString != NULL){
[orientationList addObject:orientationString];
} else {
NSString *noOrientation = [[NSString alloc] init];
noOrientation = #"No orientation avaiable";
[dateList addObject:noOrientation];
}
NSString *dateString = [[[asset defaultRepresentation] metadata] objectForKey:#"DateTime"];
if(dateString != NULL){
[dateList addObject:dateString];
} else {
NSString *noDate = [[NSString alloc] init];
noDate = #"No date avaiable";
[dateList addObject:noDate];
}
}
}];
}
if (group == nil){
[xmlWriter writeStartDocumentWithEncodingAndVersion:#"UTF-8" version:#"1.0"];
[xmlWriter writeStartElement:#"Data"];
int i = 0;
for(i = 0; i<=[idList count]-1; i++){
[xmlWriter writeStartElement:#"Photo"];
[xmlWriter writeAttribute:#"Id" value:[idList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Width" value:[widthList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Height" value:[heightList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Orientation" value:[orientationList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Date" value:[dateList objectAtIndex:i]];
[xmlWriter writeEndElement:#"Photo"];
}
[xmlWriter writeEndElement:#"Data"];
[xmlWriter writeEndDocument];
xmlList = [xmlWriter toString];
}
} failureBlock:^(NSError *error) {
NSLog(#"error enumerating AssetLibrary groups %#\n", error);
}];
return xmlList;
}
Ok at the end i have resolve my problem. I have used the NSConditionLock Class. Here there is the updated code:
-(NSString *) getPhotos{
enum { WDASSETURL_PENDINGREADS = 1, WDASSETURL_ALLFINISHED = 0};
NSMutableArray *idList = [[NSMutableArray alloc] init];
NSMutableArray *widthList = [[NSMutableArray alloc] init];
NSMutableArray *heightList = [[NSMutableArray alloc] init];
NSMutableArray *orientationList = [[NSMutableArray alloc] init];
NSMutableArray *dateList = [[NSMutableArray alloc] init];
__block XMLWriter* xmlWriter = [[XMLWriter alloc]init];
__block NSString *xmlList;
__block NSString *test;
__block NSConditionLock * assetsReadLock = [[NSConditionLock alloc] initWithCondition:WDASSETURL_PENDINGREADS];
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSString *description = [[NSString alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSString *description = [asset description];
NSRange first = [description rangeOfString:#"URLs:"];
NSRange second = [description rangeOfString:#"?id="];
NSString *path = [description substringWithRange: NSMakeRange(first.location + first.length, second.location - (first.location + first.length))];
[idList addObject:path];
NSDictionary *data = [[asset defaultRepresentation] metadata];
NSNumber *width = [[[asset defaultRepresentation] metadata] objectForKey:#"PixelWidth"];
NSString *widthString = [NSString stringWithFormat:#"%#", width];
[widthList addObject:widthString];
NSNumber *height = [[[asset defaultRepresentation] metadata] objectForKey:#"PixelHeight"];
NSString *heightString = [NSString stringWithFormat:#"%#", height];
[heightList addObject:heightString];
NSNumber *orientation = [[[asset defaultRepresentation] metadata] objectForKey:#"Orientation"];
NSString *orientationString = [NSString stringWithFormat:#"%#", orientation];
if(orientationString != NULL){
[orientationList addObject:orientationString];
} else {
NSString *noOrientation = [[NSString alloc] init];
noOrientation = #"No orientation avaiable";
[dateList addObject:noOrientation];
}
NSString *dateString = [[[asset defaultRepresentation] metadata] objectForKey:#"DateTime"];
if(dateString != NULL){
[dateList addObject:dateString];
} else {
NSString *noDate = [[NSString alloc] init];
noDate = #"No date avaiable";
[dateList addObject:noDate];
}
}
}];
}
if (group == nil){
[xmlWriter writeStartDocumentWithEncodingAndVersion:#"UTF-8" version:#"1.0"];
[xmlWriter writeStartElement:#"Data"];
int i = 0;
for(i = 0; i<=[idList count]-1; i++){
[xmlWriter writeStartElement:#"Photo"];
[xmlWriter writeAttribute:#"Id" value:[idList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Width" value:[widthList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Height" value:[heightList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Orientation" value:[orientationList objectAtIndex:i]];
[xmlWriter writeAttribute:#"Date" value:[dateList objectAtIndex:i]];
[xmlWriter writeEndElement:#"Photo"];
}
[xmlWriter writeEndElement:#"Data"];
[xmlWriter writeEndDocument];
xmlList = [xmlWriter toString];
[assetsReadLock lock];
[assetsReadLock unlockWithCondition:WDASSETURL_ALLFINISHED];
}
} failureBlock:^(NSError *error) {
NSLog(#"error enumerating AssetLibrary groups %#\n", error);
[assetsReadLock lock];
[assetsReadLock unlockWithCondition:WDASSETURL_ALLFINISHED];
}];
});
[assetsReadLock lockWhenCondition:WDASSETURL_ALLFINISHED];
[assetsReadLock unlock];
NSLog(#"XML: %#", xmlList);
return xmlList;
}

-[__NSCFString setStroke]: unrecognized selector sent to instance

I keep getting this error when I run my code, I am trying to assign attributed text to a button. I am doing assignment #2 from the standford course cs193p. The code go pass the line in question and then randonly it will crash with the error -[__NSCFString setStroke]: unrecognized selector sent to instance. The line of code in question is when I setAttributedTitle forState. Here is the code I am using
for(UIButton * setButton in self.setButtons){
Card *card = [self.game cardAtIndex:[self.setButtons indexOfObject:setButton]];
NSDictionary* fullType = #{ #"triangle" : #"▲", #"circle" : #"●", #"square" : #"■"};
NSDictionary* hollowType = #{ #"triangle" : #"△", #"circle" : #"○", #"square" : #"☐"};
NSLog(#"%#", card.contents);
NSArray * arraycontaininginfo = [card.contents componentsSeparatedByString:#","];
NSLog(#"%#", arraycontaininginfo);
NSLog(#"%#", arraycontaininginfo[2]);
NSLog(#"%#", arraycontaininginfo[3]);
if([arraycontaininginfo[2] isEqualToString:#"0.0"]){ // This helps me choose object in UIButton
NSString * displayType = [hollowType objectForKey:arraycontaininginfo[1]];
if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:#"1"]) {
self.displayView = [[NSString alloc] initWithString:displayType];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:#"2"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:#"3"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
}
} else{
NSString * displayType = [fullType objectForKey:arraycontaininginfo[1]];
if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:#"1"]) {
self.displayView = [[NSString alloc] initWithString:displayType];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:#"2"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:#"3"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
}
}
NSLog(#"%#", self.displayView);
NSLog(#"%#", arraycontaininginfo[2]);
CGFloat alphaValue = [[arraycontaininginfo objectAtIndex:2] floatValue]; // gets color from dictionary by searching key using NSString method and returns UIColor
UIColor *colorPicked = [self.colorTable objectForKey:arraycontaininginfo[3]];
NSLog(#"%#", [colorPicked class]);
NSLog(#"%#", arraycontaininginfo[3]);
NSLog(#"%#", colorPicked);
// CODE CRASHES HERE!!!
NSDictionary * attributeDictionary = #{NSForegroundColorAttributeName: [colorPicked colorWithAlphaComponent:alphaValue], NSStrokeColorAttributeName : arraycontaininginfo[3], NSStrokeWidthAttributeName: #-4};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.displayView attributes:attributeDictionary];
[setButton setAttributedTitle:attributedString forState:UIControlStateSelected];
[setButton setAttributedTitle:attributedString forState:UIControlStateNormal];
}
Replace
NSStrokeColorAttributeName : arraycontaininginfo[3]
with
NSStrokeColorAttributeName : colorPicked
and see what happens.
Source:
NSStrokeColorAttributeName
NSColor
Default nil, same as foreground color
Available in OS X v10.3 and later. Declared in NSAttributedString.h.
From documentation

Fix unicode characters in NSString

I am getting a string response from the Google Directions API that contains characters that are supposed to represent a different-language character. Similiar to Ƨ³, similiar to these type of characters. I think the characters are portugese, but anyways my string contains like this:
\U00e3o
(I am not sure if those are zeroes or 'O's)
I am not sure what the technical term for these characters are, but how can I fix them in my string so they print properly.
Thank you
UPDATE:
I have updated my question title with the correct term 'unicode'.
I have checked a few questions:
NSString Unicode display
Detect Unicode characters in NSString on iPhone
iOS HTML Unicode to NSString?
And a few others. I have followed the answer, but the unicode characters are not fixed.
UPDATE:
Here is my code to get the response from GDirection.
Forming the request and getting a response:
AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
[_httpClient setDefaultHeader:#"Accept" value:#"application/json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[NSString stringWithFormat:#"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:#"origin"];
[parameters setObject:[NSString stringWithFormat:#"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:#"destination"];
[parameters setObject:#"false" forKey:#"sensor"];
[parameters setObject:#"driving" forKey:#"mode"];
[parameters setObject:#"metric" forKey: #"units"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:#"GET" path: #"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200) {
[self parseResponse:responseObject];
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[_httpClient enqueueHTTPRequestOperation:operation];
}
Retrieving information from response object:
- (void)parseResponse:(NSDictionary *)response {
NSString *status = [response objectForKey: #"status"];
if (![status isEqualToString: #"OK"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[NSString stringWithFormat: #"Google Directions Response Status: %#", status] delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
}
else {
NSArray *routes = [response objectForKey:#"routes"];
NSDictionary *routePath = [routes lastObject];
if (routePath) {
NSString *overviewPolyline = [[routePath objectForKey: #"overview_polyline"] objectForKey:#"points"];
legs = [routePath valueForKey: #"legs"];
if (legs) {
/* DIRECTION SET ================================================================================================================================
*/
directionOverview = [[NSMutableDictionary alloc] init];
NSString *legsDis = [NSString stringWithFormat: #"%#", [[legs valueForKey: #"distance"] valueForKey: #"text"]];
NSString *kmDistance = [self cutStringToPreference: legsDis];
if (kmDistance) {
[directionOverview setObject:kmDistance forKey: #"distance"];
milesLabel.text = kmDistance;
milesLabel.font = [UIFont fontWithName:#"interstate" size: 20.0];
}
NSString *durationText = [NSString stringWithFormat: #"%#", [[legs valueForKey: #"duration"] valueForKey: #"text"]];
durationText = [self cutStringToPreference: durationText];
if (durationText) {
[directionOverview setObject:durationText forKey: #"duration"];
}
NSString *startAddress = [NSString stringWithFormat: #"%#", [legs valueForKey: #"start_address"]];
startAddress = [self cutStringToPreference: startAddress];
NSString *endAddress = [NSString stringWithFormat: #"%#", [legs valueForKey: #"end_address"]];
endAddress = [self cutStringToPreference: endAddress];
[directionOverview setObject:startAddress forKey: #"origin"];
[directionOverview setObject:endAddress forKey: #"destination"];
NSArray *steps = [legs valueForKey: #"steps"];
if (steps) {
instructionArray = [[NSMutableArray alloc] init];
durationArray = [[NSMutableArray alloc] init];
distanceArray = [[NSMutableArray alloc] init];
int number = [[[steps lastObject] valueForKey: #"html_instructions"] count];
for (int i = 1; i <= number; ++i) {
NSString *instruction = [[[steps lastObject] valueForKey: #"html_instructions"] objectAtIndex: i-1];
instruction = [self cutStringToPreference: instruction];
instruction = [self flattenHTML: instruction];
instruction = [self stringByDecodingHTMLEntitiesInString: instruction];
[instructionArray addObject: instruction];
NSString *distance = [[[[steps lastObject] valueForKey: #"distance"] objectAtIndex: i-1] valueForKey: #"text"];
[distanceArray addObject: distance];
NSString *duration = [[[[steps lastObject] valueForKey: #"duration"] objectAtIndex: i-1] valueForKey: #"text"];
[durationArray addObject: duration];
}
}
}
_path = [self decodePolyLine:overviewPolyline];
NSInteger numberOfSteps = _path.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_path objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
}
}
Displaying the text in a label:
NSString *overviewAddressText = [NSString stringWithFormat: #"%# to %#", [directionOverview objectForKey: #"origin"], [directionOverview objectForKey: #"destination"]];
overviewAddress.text = overviewAddressText;
UPDATE:
Image of label
So as you can see, in the label, the text contains this kind of substring: S/U00e3o, which is a word that has an unsupported character. How can I fix that so that unicode turns into this: São

How to Add Multiple Placemarks and Annotations to Map

I have a class that loads a map, get my current location, then does a search on my database for companies in an area based on a zip code radius. I am doing a for loop to loop through each address, forward geocode, now I want to put a placemark and annotation for each location. How can I accomplish this. Here is my code:
- (void)locationUpdate:(CLLocation *)location {
locationLabel.text = [location description];
NSNumber *lat = [[NSString alloc] initWithFormat:#"%g", location.coordinate.latitude];
float latValue = [lat floatValue];
NSNumber *lng = [[NSString alloc] initWithFormat:#"%g", location.coordinate.longitude];
float lngValue = [lng floatValue];
mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.showsUserLocation=TRUE;
mapView.mapType=MKMapTypeStandard;
mapView.delegate=self;
/*Region and Zoom*/
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location1=mapView.userLocation.coordinate;
location1.latitude=latValue;
location1.longitude=lngValue;
region.span=span;
region.center=location1;
/*Geocoder Stuff*/
geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location1];
geoCoder.delegate=self;
[geoCoder start];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
[self.view insertSubview:mapView atIndex:0];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geoCoder didFindPlacemark:(MKPlacemark *)placemark
{
NSDictionary *zipDic = [placemark addressDictionary];
NSString *zipCode = [zipDic objectForKey:#"ZIP"];
NSString *post = [NSString stringWithFormat:#"zip=%#", zipCode];
NSString *hostString = #"https://www.mysite.com/searchzip.php?";
// Append string and add percent escapes
hostString = [[hostString stringByAppendingString:post] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *hostURL = [NSURL URLWithString:hostString];
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL];
self.zipArray = [jsonString JSONValue];
NSLog(#"%#", zipArray);
for (NSString *sZip in zipArray) {
NSString *lblAddress = [sZip objectForKey:#"address"];
NSString *hostStr = [[#"http://maps.google.com/maps/geo?q=" stringByAppendingString:lblAddress]stringByAppendingString:#"&key=ABQIAAAA1KqXKe5yJPkX6ii6Ud K-0RSIvIZDM4KnjydqrehqKK56hFf_fxQc0uyCKoh-4i77-5B0Qfc8Gs223Q&sensor=false&output=json"];
hostStr = [hostStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *hostURL = [NSURL URLWithString:hostStr];
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL];
SBJSON *parser = [[SBJSON alloc] init];
// parse the JSON string into an object - assuming json_string is a NSString of JSON data
NSArray *object = [parser objectWithString:jsonString error:nil];
NSArray *placemarks = [object objectForKey:#"Placemark"];
NSDictionary *mark = [placemarks objectAtIndex:0];
NSDictionary *point = [mark objectForKey:#"Point"];
NSArray *coordinates = [point objectForKey:#"coordinates"];
NSNumber *lat = (NSNumber*)[coordinates objectAtIndex:0];
float latValue = [lat floatValue];
NSNumber *lon = (NSNumber*)[coordinates objectAtIndex:1];
float lonValue = [lon floatValue];
//Here is where I would put placemarks and annotations
}
}
- (void)locationError:(NSError *)error {
locationLabel.text = [error description];
}
#end
I tried...
CLLocationCoordinate2D location = {latitude: latValue, longitude: lonValue};
return location;
But obviously its wrong.
I tried creating a seperate MapAnnotation class to handle the annotations like so:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "AgencyViewController.h"
#interface MapAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *subtitletext;
NSString *titletext;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (readwrite, retain) NSString *titletext;
#property (readwrite, retain) NSString *subtitletext;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
-(void)setTitle:(NSString*)strTitle;
-(void)setSubTitle:(NSString*)strSubTitle;
and implement it like so:
CLLocationCoordinate2D newCoord = {latitude: latValue, longitude: lonValue};
MapAnnotation *addAnnotation = [[MapAnnotation alloc] initWithCoordinate:newCoord];
[addAnnotation setTitle:#"The Pin Title"];
[addAnnotation setSubTitle:#"The pin subtitle goes here"];
[mapView addAnnotation:addAnnotation];
And that didnt work either...
Figured it out, needed to add
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"Prospects"];
if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Prospects"];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
} else {
pinView.annotation = annotation;
}
return pinView;
}
Now I need to figure out how to change the color of my location pin.