0

I have a json file and in this json I have different latitude and longitude and status. I want to check my status and based on lat and long show status pin on my map. I mean if my status is active : green pin if my status is expired show Red if my status is confidential show gray pin.

Here is my json :

[
{
"status": "active",
"latitude": 56.715866,
"longitude": -118.281757
},
{
"status": "expired",
"latitude": 56.715860,
"longitude": -118.281757
},
{
"status": "confidential",
"latitude": 56.715111,
"longitude": -118.281117
 }]

I can print all json on my log here is my method :

-(void)getPin
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request 
returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply
length] encoding: NSASCIIStringEncoding];



NSLog(@"Reply: %@", theReply);
}

I have 3 pin image in my xcode I also have mapView. My question is how to show this 3 pins based on my json on map?

Thanks in advance! Appreciate if you can provide me the code

veovo
  • 191
  • 7
  • Do you know how to parse the Jason File? If you know, you can go on read my answer. My answer is about how to add annotations. – ukim Jul 20 '14 at 04:37
  • Sample code to add annotations from JSON: http://stackoverflow.com/questions/14802618/ios-json-array-and-mapkit. For the pin color, add the "status" property to your custom annotation class (as shown in answer by @Sen) and implement the viewForAnnotation delegate method as shown here: http://stackoverflow.com/questions/24215210/does-mkannotationview-buffer-its-input-queue. –  Jul 21 '14 at 10:59

1 Answers1

0

I have made this demo project for you.

In order to show your own annotation, you need first to make a class that confirms MKAnnotation protocol. Let's call this class Location. You need to add a instance of this class into the map to tell the map the position of your annotation. You can optionally add title and subtile into this class to tell the map the title and subtitle of the annotation.

@interface Location : NSObject<MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;

- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subTitle:(NSString *)subTitle;

@end

Now go to the view controller that contains the map. Create and add an annotation into the map. Also, set the delegate of the map to self:

Location * myLocation = [[Location alloc]initWithCoordinate:CLLocationCoordinate2DMake(56.715866, -118.281757) title:@"My Land" subTitle:@"haha~"];
[_mapView addAnnotation:myLocation];
_mapView.delegate = self;

The controller should confirm to MKMapViewDelegate in order to show custom annotation. The method that tell the map view how to display an annotation is:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
    MKAnnotationView * view = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Location"];
    view.image = [UIImage imageNamed:@"someImage"];
    view.canShowCallout = YES;
    return view;
}

You can set view.canShowCallout = YES;, so that when you touch the annotation, it will show the title and subtile in your annotation.

If you have more than one annotations, it will be convenient to add a property @property (nonatomic, strong) UIImage * image into your Location class. Which can hold the image of the annotation, and in mapView:viewForAnnotation: set view.image = annotation.image.

ukim
  • 2,395
  • 15
  • 22
  • thank you for your comment, did you read my question,I said that I can read json, and also my question is how to show them from json – Sara Jurdan Jul 20 '14 at 06:37
  • @Sara Jurdan You can convert the Json data into an array of Locations. And add them into the map using the method I mentioned above. – ukim Jul 20 '14 at 06:46