Reputation: 45
I am trying to place a custom annotation on a map at coordinates taken from an NSArray using mapKit. The annotation does not appear when I run the app though. I have checked using NSLog, that both latitude and longitude are present.
I'm not sure where it is going wrong. There is another method in the class that prints an annotation successfully using the devices location, the difference is in this method I want to use a custom location with CLLocationCoordinate2D.
This is the method from the .m file:
#import "MapViewController.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize mapView=_mapView;
- (void)place:(NSArray *)tweetData
{
NSArray *array = tweetData;
NSDictionary *dict = [array objectAtIndex:0];
NSString *lat = [dict objectForKey:@"lat"];
NSString *lon = [dict objectForKey:@"lon"];
double lat2 = [lat doubleValue];
double lon2 = [lon doubleValue];
NSLog(@"String %@, %@", lat, lon);
NSLog(@"Double %.8f, %.8f", lat2, lon2);
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(lat2, lon2);
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = position;
annotation.title = @"working";
annotation.subtitle = @"working";
[_mapView addAnnotation:annotation];
}
Update: Just to clarify this other method in the same class works fine. I just want to use a different location that I've taken from the array. The default annotation is fine I just want to change the location.
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}
Update 2: This is the other class which contains the call to place:
#import "ViewController.h"
#import "MapViewController.h"
#import <CoreLocation/CoreLocation.h>
@implementation ViewController
@synthesize button=_button;
@synthesize label=_label;
@synthesize tweetId=_tweetId;
@synthesize tweetContent=_tweetContent;
@synthesize connection=_connection;
@synthesize mapView=_mapView;
NSString *tweet;
NSMutableArray *locationArray;
- (IBAction)fetchTweet
{
NSString *sendCo = [[locationArray valueForKey:@"description"] componentsJoinedByString:@","];
NSLog(@"sendCo: %@", sendCo);
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/Jersey/rest/hello"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/html" forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody:[sendCo dataUsingEncoding:NSUTF8StringEncoding]];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
tweet = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:[tweet dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
NSLog(@"%@", jsonData);
_mapView = [[MapViewController alloc] init];
[_mapView place:jsonData];
}
Upvotes: 2
Views: 1447
Reputation: 521
As igor says, you should implement MKMapViewDelegate protocol, set your controller as the MKMapView delegate and implement at least the next method:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString* PinReuseIdentifier = @"YourReuseIdentifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: PinReuseIdentifier];
if (!annotationView){
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: PinReuseIdentifier];
}
//Customize annotationView
return annotationView;
}
And provide your custom pin view inside MKAnnotationView
Upvotes: 1