Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created September 14, 2012 07:00
Show Gist options
  • Save ChrisRisner/3720365 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3720365 to your computer and use it in GitHub Desktop.
GeoDemo-ios2
- (void)fetchedData:(NSData *)responseData {
NSError* error;
//Build a JSON object from the response Data
NSArray* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
//Go through each POI in the JSON data and pull out the important fields
for (NSDictionary *pointOfInterest in json) {
NSLog(@"POI:%@", pointOfInterest);
CLLocationCoordinate2D mapPoint = mapView.centerCoordinate;
NSString *latString = [pointOfInterest valueForKey:@"Latitude"];
NSString *longString = [pointOfInterest valueForKey:@"Longitude"];
NSString *description = [pointOfInterest valueForKey:@"Description"];
NSString *url = [pointOfInterest valueForKey:@"Url"];
mapPoint.latitude = [latString doubleValue];
mapPoint.longitude = [longString doubleValue];
//Add a new map annotation for each POI
MKPointAnnotation *anny = [[MKPointAnnotation alloc] init];
anny.coordinate = mapPoint;
anny.title = description;
anny.subtitle = url;
[mapView addAnnotation:anny];
}
}
- (void)getCurrentPointsOfInterest {
NSURL *requestUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?latitude=%f&longitude=%f&radiusInMeters=1000",kGetPOIUrl, [currentLocation coordinate].latitude, [currentLocation coordinate].longitude]];
//Get our POI
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
requestUrl];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (IBAction)tapRefresh:(id)sender {
[self getCurrentPointsOfInterest];
}
- (void)locationError:(NSError *)error {
labelLatitude.text = [error description];
labelLongitude.text = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self getCurrentPointsOfInterest];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *AnnotationViewID = @"annotationViewID";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
return annotationView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment