Hi,
In my App, I am using a UIMapView to display placemarks at certain locations from my JSON.
My problem is is that say I have 20 locations only 1 (sometimes 2-3) usually the last few are being shown on the map?!
here is my code:
In my App, I am using a UIMapView to display placemarks at certain locations from my JSON.
My problem is is that say I have 20 locations only 1 (sometimes 2-3) usually the last few are being shown on the map?!
here is my code:
Code:
//
// MapViewController.m
// pubs
//
// Created by John Biddulph on 10/04/2014.
// Copyright (c) 2014 John Biddulph. All rights reserved.
//
#import "MapViewController.h"
#import "PubObject.h"
#import "PubsViewController.h"
@interface MapViewController (){
NSString *thisTown;
NSString *pID;
NSString *pName;
NSString *pAddress;
NSString *pAdd2;
NSString *pAdd3;
NSString *pTown;
NSString *pCounty;
NSString *pRegion;
NSString *pWebsite;
NSString *pTel;
NSString *pAbout;
}
@end
@implementation MapViewController
@synthesize regionCourante, radius, centre, centre1, accuracy, activityIndicator, locationManager, pubsArray, jsonArray, currentPub, mapTown, infoRequest, pubAmount;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
infoRequest.text = mapTown;
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
self.locationManager = [[CLLocationManager alloc]init];
self.accuracy = 10.0;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[super viewDidLoad];
_mapView.zoomEnabled = YES;
_mapView.scrollEnabled = YES;
_mapView.showsUserLocation = YES;
[_mapView setUserTrackingMode:MKUserTrackingModeFollow];
}
-(void)loading{
if(!jsonArray.lastObject){
[Indicator startAnimating];
} else {
[Indicator stopAnimating];
}
[self loadmap];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)getPub:(id)townObject
{
currentPub = townObject;
}
-(void)loadmap{
thisTown = mapTown;
[Indicator removeFromSuperview];
[timer invalidate];
timer = nil;
NSLog(@"this is the selected town:%@",mapTown);
NSString *thisTown1 = [NSString stringWithFormat:@"http://www.mypubspace.com/pubsmobile/pubstown.php?rsTown=%@", mapTown];
NSLog(@"here is the link:%@",thisTown1);
NSURL * url = [NSURL URLWithString:thisTown1];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
pubsArray = [[NSMutableArray alloc] init];
[self.activityIndicator startAnimating];
for(int i = 0; i< jsonArray.count; i++){
NSString *pPostcode1 = [[jsonArray objectAtIndex:i] objectForKey:@"rsPostCode"];
[pubsArray addObject:[[PubObject alloc]initWithPubName:pName
andTown:pTown andPostCode:pPostcode andPubID:pID andAddress: pAddress andAdd2: pAdd2 andAdd3: pAdd3 andTel: pTel andWebsite: pWebsite andCounty: pCounty andRegion: pRegion andAbout: pAbout]];
location = pPostcode1;
MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
[directionsRequest setSource:source];
[directionsRequest setTransportType:1 << 1];
geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:pPostcode1
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
topResult = [placemarks objectAtIndex:0];
placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
destination = placemark.location;
CLLocationCoordinate2D coordinate = destination.coordinate;
destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
thisdestination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
[directionsRequest setDestination:thisdestination];
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
self.activityIndicator.hidden = YES;
[self.activityIndicator stopAnimating];
if (error) {
NSLog(@"There was an error getting your directions");
return;
}
MKCoordinateRegion region = _mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 2000.0;
region.span.latitudeDelta /= 2000.0;
[_mapView setRegion:region animated:YES];
[_mapView addAnnotation:placemark];
centre1 = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
float theLat = placemark.location.coordinate.latitude;
float theLong = placemark.location.coordinate.longitude;
theUrl = [NSString stringWithFormat:@"http://maps.google.com/?cbll=%f,%f&cbp=12,20.09,,0,5&layer=c",theLat,theLong];
}];
}
}
];
}
NSLog(@"There are %lu pubs in %@ here is a postcode %@",(unsigned long)pubsArray.count, mapTown, pPostcode);
}
@end