Multiple annotations not showing in Iphone mapview - iphone

I have looked at other questions in stackoverflow that are closely related to my question however my issue is a bit different. Here, the code works fine on iPad but not my iPhone. My iPad will load all 300+ annotations correctly but my iPhone will only load only 80 of them. I'm not sure if this is due to the amount of memory on the iPhone because it does the same thing when I test on the simulator. Below you will find my sample code.
#interface MOVmapViewController ()
#end
#define VA_LATITUDE 37.413754;
#define VA_LONGITUDE -79.142246;
//Span
#define THE_SPAN 5.50f;
#implementation MOVmapViewController
#synthesize myMapView;
-(IBAction)findmylocation:(id)sender {
myMapView.showsUserLocation = YES;
myMapView.delegate = MKMapTypeStandard;
[myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
-(IBAction)setmaptype:(id)sender {
switch (((UISegmentedControl *)sender).selectedSegmentIndex) {
case 0:
myMapView.mapType = MKMapTypeStandard;
break;
case 1:
myMapView.mapType = MKMapTypeSatellite;
break;
case 2:
myMapView.mapType = MKMapTypeHybrid;
break;
default:
myMapView.mapType = MKMapTypeStandard;
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typiclly from a nib.
//Create the region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = VA_LATITUDE;
center.longitude = VA_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
//Set our mapView
[myMapView setRegion:myRegion animated:YES];
//Annotations
//Abingdon Lodge No. 48
NSString *abingdon = #"325 W Main Street Abingdon, Virginia 24210";
CLGeocoder *abingdongeo = [[CLGeocoder alloc] init];
[abingdongeo geocodeAddressString:abingdon completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"Abingdon Lodge No. 48";
point.subtitle = #"Abingdon, VA";
[self.myMapView addAnnotation:point]; }}];
//York Lodge No. 12
NSString *york = #"14411 Black Hollow Road Abingdon, Virginia 24210";
CLGeocoder *yorkgeo = [[CLGeocoder alloc] init];
[yorkgeo geocodeAddressString:york completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"York Lodge No. 12";
point.subtitle = #"Abingdon, VA";
[self.myMapView addAnnotation:point]; }}];
//Alberene Lodge No. 277
NSString *alberene = #"2722 Plank Road Alberene, Virginia 22959";
CLGeocoder *alberenegeo = [[CLGeocoder alloc] init];
[alberenegeo geocodeAddressString:alberene completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"Alberene Lodge No. 277";
point.subtitle = #"Alberene, VA";
[self.myMapView addAnnotation:point]; }}];
//A. Douglas Smith, Jr. Lodge of Research No. 1949
NSString *douglas = #"101 Callahan Drive Alexandria, Virginia 22301";
CLGeocoder *douglasgeo = [[CLGeocoder alloc] init];
[douglasgeo geocodeAddressString:douglas completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"A. Douglas Smith, Jr. Lodge of Research No. 1949";
point.subtitle = #"Alexandria, VA";
[self.myMapView addAnnotation:point]; }}];
//Alexandria-Washington Lodge No. 22 same location as above use lat/long
CLLocationCoordinate2D washington;
washington.latitude = 38.806758;
washington.longitude = -77.065251;
Annotation *alexandriawashingtonlodge = [Annotation alloc];
alexandriawashingtonlodge.coordinate = washington;
alexandriawashingtonlodge.title = #"Alexandria-Washington Lodge No. 22";
alexandriawashingtonlodge.subtitle = #"Alexandria, VA";
[self.myMapView addAnnotation:alexandriawashingtonlodge];
This list continues for a total of about 310 lodge locations.

Use like this,
NSString *abingdon = #"325 W Main Street Abingdon, Virginia 24210";
CLGeocoder *abingdongeo = [[CLGeocoder alloc] init];
[abingdongeo geocodeAddressString:abingdon completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"Abingdon Lodge No. 48";
point.subtitle = #"Abingdon, VA";
// Set your region using placemark (not point)
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
// Add point (not placemark) to the mapView
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:point];
// Select the PointAnnotation programatically
[self.mapView selectAnnotation:point animated:NO]; }}];

The reason that some of my annotations are not showing up is because I'm calling on CLGeocoder to many times. Here is how I fixed the issue.
//Columbia Lodge No. 285
CLLocationCoordinate2D columbia;
columbia.latitude = 38.895746;
columbia.longitude = -77.106511;
Annotation *columbialodge = [[Annotation alloc] init];
columbialodge.coordinate = columbia;
columbialodge.title = #"Columbia Lodge No. 285";
columbialodge.subtitle = #"Arlington, VA";
[self.myMapView addAnnotation:columbialodge];
I'v converted all my of address to Lat long. Which has cut down on memory usage as well.
As much as I enjoyed just putting in the address it looks like CLGeocoder is just taking up to many resources.

Related

Load Map View on MKMapView

I want to load map on MKMapView.
Basically what i want to do is,
I want to load Particular Venue in my MapView.
My Database contains lot of Venues, according to requirement, i am fetching Venue and i want to load that Venue in my mapView.
for eg: I got this as Venue: #"100 Oxford Street, London, W1D 1LL, 020 7636 0933" from Database, then i want to load this location in my mapView
Thanks in advance.
EDIT:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *venue= #"100 Oxford Street, London, W1D 1LL, 020 7636 0933";
[geocoder geocodeAddressString:venue completionHandler:^(NSArray *placemarks, NSError *error)
{
if ([placemarks count] > 0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *loc = placemark.location;
CLLocationCoordinate2D _venue = loc.coordinate;
NSLog(#"_venue:=%f",loc.coordinate);
MKPointAnnotation *venueAnnotation = [[MKPointAnnotation alloc]init];
[venueAnnotation setCoordinate:_venue];
[venueAnnotation setTitle:#"Venue"];
[MapVw addAnnotation:venueAnnotation];
}
}
];
- (IBAction)BtnClick:(id)sender {
NSLog(#"Map ");
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:#"100 Oxford Street, London, W1D 1LL, 020 7636 0933"
completionHandler:^(NSArray* placemarks, NSError* error)
{
if (placemarks && placemarks.count > 0)
{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
[self.MapView addAnnotation:placemark];
CLLocationCoordinate2D _venue = placemark.coordinate;
[self.MapView setCenterCoordinate:_venue];
MKCoordinateRegion region = self.MapView.region;
region.span.longitudeDelta = 1.0;
region.span.latitudeDelta = 1.0;
[self.MapView setRegion:region animated:YES];
}
}
];
}
Steps:
1. Import CoreLocation.framework and MapKit.framework.
2. Use CoreLocation class Geocoder's method :
geoCodeAddressString:completionHandler:
NSString *venue= #"100 Oxford Street, London, W1D 1LL, 020 7636 0933";
[_geoCoder geocodeAddressString:venue completionHandler:^(NSArray *placemarks, NSError *error)
{
if ([placemarks count] > 0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *loc = placemark.location;
CLLocationCoordinate2D _venue = loc.coordinate;
}
});
]
[self performSelector:#selector(createVenueAnnotation) withObject:nil afterDelay:5];
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]init];
[_mapView addSubview:activity];
[activity startAnimating];
Step 3. If you want to place a MKPointAnnotation on the venue point.
- (void)createVenueAnnotation{
[activity stopAnimating];
[activity removeFromSuperview];
MKPointAnnotation *venueAnnotation = [[MKPointAnnotation alloc]init];
[venueAnnotation setCoordinate:_venue];
[venueAnnotation setTitle:#"Venue"];
[_mapView addAnnotation:venueAnnotation];
}
Step 4: Center your map around the venue.
// you need to call this function
- (void)centerMapAroundVenue
{
MKMapRect rect = MKMapRectNull;
MKMapPoint venuePoint = MKMapPointForCoordinate(_venue);
rect = MKMapRectUnion(rect, MKMapRectMake(venuePoint .x, venuePoint .y, 0, 0));
MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
[_mapView setRegion:region animated:YES];
}
Here is simple scenario :
Get the Co-ordinates of the Address using GeoCoder
Using the Co-ordinatation ,Add the Annotation to the MapView.
Do something Like this to get the CLlocationCoordinate :
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%#", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:#"\"lat\" :" intoString:nil] && [scanner scanString:#"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:#"\"lng\" :" intoString:nil] && [scanner scanString:#"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
Using the Center just add the annotation to the MapView.
[self.mapView addAnnotation:<annotationName>];
Plz go through this link For more info.
Hope this helps.

iOS: MapKit: How can i set the number of annotations that search request fetches

I have the following code for retrieving restaurants inside my application
NSLog(#"getting restaurants");
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = #"restaurant";
request.region = self.mapView.region;
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
NSMutableArray *annotations = [NSMutableArray array];
[response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithPlacemark:item.placemark];
annotation.title = item.name;
annotation.coordinate = item.placemark.location.coordinate;
//annotation.subtitle = item.placemark.addressDictionary[kABPersonAddressStreetKey];
//annotation.phone = item.phoneNumber;
[annotations addObject:annotation];
}];
[self.mapView addAnnotations:annotations];
}];
How can i set the limit for how many annotations to be loaded ?
Now, only a handfull of the available points are loaded. When i zoom in further, i call the code again, and additional points appear.

Can you create an annotation in Mapview from an address?

I have "googled" this question a few times today with no luck. I would like to know if it is possible to create an annotation in Mapview from an address rather than using the lat/Long method. My current code is below.
//1. Create a coordinate for use with the annotation
CLLocationCoordinate2D Sharon;
Sharon.latitude = 38.952223;
Sharon.longitude = -77.193646;
Annotation *myAnnotation = [Annotation alloc];
myAnnotation.coordinate = Sharon;
myAnnotation.title = #"Sharon Lodge #327";
myAnnotation.subtitle = #"McLean, VA";
Try like this,
NSString *location = "some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
];
for annotation title and subtitle,
CLPlacemark *topResult = [placemarks objectAtIndex:0];
// Create an MLPlacemark
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
// Create an editable PointAnnotation, using placemark's coordinates, and set your own title/subtitle
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"Sample Location";
point.subtitle = #"Sample Subtitle";
// Set your region using placemark (not point)
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
// Add point (not placemark) to the mapView
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:point];
// Select the PointAnnotation programatically
[self.mapView selectAnnotation:point animated:NO];
You can get lat long from an address using google api or CLGeocoder
google api
Link 1 Link 2
CLGeocoder
-(id)getAnnotationAryFromAddress
{
CLGeocoder *geocoder2 = [[CLGeocoder alloc] init];
NSString *addressString = "string contain address, city, state and zip";
[geocoder2 geocodeAddressString:addressString
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
//NSLog(#"Geocode failed with error: %#", error);
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
RegionAnnotation *annotation = [[RegionAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1 = location.coordinate;
annotation.title = #"Title";
annotation.subtitle = #"SubTitle";
annotation.tag = i;
annotation.coordinate = theCoordinate1;
[self.mapView addAnnotation:annotation];
}
}];
}
return self;
}

MapView annotations/pins in iPhone

In my viewDidLoad: method I have set the MapView like this so that if user has selected all the locations then it should show all the locations/pin views and if only one then only one pin view should be shown.
- (void)viewDidLoad{
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
if ([vehicleLabel.text isEqualToString:#"All Vehicles"]) {
MKCoordinateRegion region = {{0.0, 0.0},{0.0, 0.0}};
region.center.latitude = 41.01860;
region.center.longitude = 28.96470;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
Annotations *ann = [[Annotations alloc] init];
ann.title = #"Vehicle A";
ann.subtitle = #"VG 1";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = 41.01760;
theCoordinate1.longitude = 30.96470;
CLLocationCoordinate2D theCoordinate2;
theCoordinate2.latitude = 41.01860;
theCoordinate2.longitude = 31.96470;
CLLocationCoordinate2D theCoordinate3;
theCoordinate3.latitude = 41.01360;
theCoordinate3.longitude = 25.96470;
CLLocationCoordinate2D theCoordinate4;
theCoordinate4.latitude = 41.01560;
theCoordinate4.longitude = 27.96470;
Annotations* myAnnotation1=[[Annotations alloc] init];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=#"Vehicle B";
myAnnotation1.subtitle=#"Group 1";
Annotations* myAnnotation2=[[Annotations alloc] init];
myAnnotation2.coordinate=theCoordinate2;
myAnnotation2.title=#"Vehicle C";
myAnnotation2.subtitle=#"Group 1";
Annotations* myAnnotation3=[[Annotations alloc] init];
myAnnotation3.coordinate=theCoordinate3;
myAnnotation3.title=#"Vehicle D";
myAnnotation3.subtitle=#"Group 1";
Annotations* myAnnotation4=[[Annotations alloc] init];
myAnnotation4.coordinate=theCoordinate4;
myAnnotation4.title=#"Vehicle E";
myAnnotation4.subtitle=#" Group 1";
[mapView addAnnotation:myAnnotation1];
[mapView addAnnotation:myAnnotation2];
[mapView addAnnotation:myAnnotation3];
[mapView addAnnotation:myAnnotation4];
[annotations addObject:myAnnotation1];
[annotations addObject:myAnnotation2];
[annotations addObject:myAnnotation3];
[annotations addObject:myAnnotation4];
NSLog(#"Annotations: %d",[annotations count]);
}
else {
MKCoordinateRegion region = {{0.0, 0.0},{0.0, 0.0}};
region.center.latitude = 41.01860;
region.center.longitude = 28.96470;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
Annotations *ann = [[Annotations alloc] init];
ann.title = #"Vehicle A";
ann.subtitle = #"VG 1";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}}
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"aPin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
} else {
}
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
return pinView;}
And in my PickerView didSelectRow method: I have implemented:
-(void)pickerView:(UIPickerView *)pickerViewG didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component == kGroupComponent) {
NSString *selectedState = [self.vehicleGroup objectAtIndex:row];
NSArray *array = [groupVehicles objectForKey:selectedState];
self.vehiclesList = array;
[vehiclePicker selectRow:0 inComponent:kListComponent animated:YES];
[vehiclePicker reloadComponent:kListComponent];
}
NSInteger groupRow = [vehiclePicker selectedRowInComponent:kGroupComponent];
NSInteger vehicleRow = [vehiclePicker selectedRowInComponent:kListComponent];
NSString *group = [self.groupOfVehicles objectAtIndex:groupRow];
NSString *vehicle = [self.listVehicles objectAtIndex:vehicleRow];
groupLabel.text = [[NSString alloc] initWithFormat: #"%#", group];
vehicleLabel.text = [[NSString alloc] initWithFormat: #"%#", vehicle];
valueForOtherView = vehicleLabel.text;}
Here what I want to know is that what if user selects a single vehicle from the picker view then all other pin views/ locations should be hidden/removed from the view and the only selected location/ pin view should be shown.
How can I do that in this picker view didSelectRow method: ?
What I'm doing in a case similar to yours, is:
1) remove first all the annotations
for (id <MKAnnotation> anAnnotation in [NSArray arrayWithArray:mapView_.annotations]) {
[mapView_ removeAnnotation:anAnnotation];
}
2) and then paint the ones selected:
[mapView_ addAnnotation:myAnnotation];

How to update Mapview Periodically?

In my application i want to update the MapView periodically and redraw the the path on map as per new location.
Is it possible using MKMapView?
This is my didload metho for drawing and annotation on mapview.
- (void)viewDidLoad
{
NSString *url=[NSString stringWithFormat:#"xxxxx.php?uid=%#&buddy_uid=%#",UserUID,buddyUid];
NSLog(#"%#",url);
NSLog(#"%#",url);
NSString * jsonString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"return data %#",jsonString);
NSDictionary *jsonDic = [jsonString JSONValue];
buddyLocation = [[NSString alloc] initWithFormat:#"%#,%#",[jsonDic objectForKey:#"Lattitude"],[jsonDic objectForKey:#"Longitude"]];
[super viewDidLoad];
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
//[locmanager startUpdatingLocation];
MKMapView *mapnew;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
mapnew = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
}
else
{
mapnew = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
}
[mapnew setDelegate:self];
[self.view addSubview:mapnew];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = lat1 floatValue];//22.56574;
region.center.longitude =lng1 floatValue];//73.74575;
NSString *currentLocation = [[NSString alloc] initWithFormat:#"%#,%#",lat1,lng1];
NSLog(#"%#",currentLocation);
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #"Current Location";
ann.coordinate = region.center;
[mapnew addAnnotation:ann];
NSLog(#"%#",jsonDic);
region.center.latitude = [[jsonDic objectForKey:#"Lattitude"] floatValue];//23.56574;
region.center.longitude = [[jsonDic objectForKey:#"Longitude"] floatValue];//72.74575;
ann = [[DisplayMap alloc] init];
ann.title = #"Buddy Location";
ann.coordinate = region.center;
[mapnew addAnnotation:ann];
KMLParser *kml = [KMLParser parseKMLAtURL:[NSURL URLWithString:[NSString
stringWithFormat:#"https://maps.google.com/maps?saddr=%#&daddr=%#&
output=kml",currentLocation,buddyLocation]]];
NSArray *overlays = [kml overlays];
[mapnew addOverlays:overlays];
MKMapRect flyTo = MKMapRectNull;
for (id <MKOverlay> overlay in overlays)
{
if (MKMapRectIsNull(flyTo))
{
flyTo = [overlay boundingMapRect];
}
else
{
flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
}
}
mapnew.visibleMapRect = flyTo;
[mapnew release];
mapnew =nil;
}
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *line = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
line.strokeColor = [UIColor blueColor];
line.lineWidth =2.0;
return line;
}
ViewDidiLoad method is called only once when the view is loaded After that it would only give control to methods like viewWillappear etc on successive visits . What you have to do is seperate the method which parses the location data from viewDidLoad, and make it set an array or objects . And call the method to reset these contents on Map i.e. remove previous annotations or overlay and then add new ones . problem solved. You could call these method at fixed interval using timer or something like that. I guess this should help.