MapView annotations/pins in iPhone - 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];

Related

MKPointAnnotation: title always visible. Is it possible?

My code for adding a marker and title is this:
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = coord;
annotationPoint.title = currentTitle;
[mapView addAnnotation:annotationPoint];
Is it possible to show title on marker immediately after marker is shown and always visible ?
Thanks much
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = coord;
annotationPoint.title = currentTitle;
[mapView addAnnotation:annotationPoint];
[mapView selectAnnotation:annotationPoint animated:NO];
OR
- (void)mapView:(MKMapView *)map didAddAnnotationViews:(NSArray *)views{
for (MKAnnotationView *av in views){
if ([av.annotation isKindOfClass:[MKPointAnnotation class]]){
[mapView selectAnnotation:av.annotation animated:NO];
break;
}
}
}

Adding multiple annotations to iPhone MapView is slow

I have code that takes a title and an address of a location,then get coordinates from google maps csv, then add annotation. It works, but when I want to add 50 or more annotation at once it freezes for about 30-40 seconds and then displays annotations, so what I want to know is did I screw up memory management and that's why it's so slow, or something else? Can I speed it up?
Here is the code:
- (void) addAnnotation: (NSString*) annoTitle: (NSString*) annoAddress
{
NSString *address = annoAddress;
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
float langcoord = [[listItems objectAtIndex:2] floatValue];
float longcoord = [[listItems objectAtIndex:3] floatValue];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(langcoord, longcoord);
// Create an instance of MapPoint with the current data
Annotation *annotation = [[Annotation alloc] initWithCoordinate:coord title:annoTitle];
// Add it to the map view
[mapView addAnnotation:annotation];
// MKMapView retains its annotations, so we can release it
[annotation release];
}
Following methods are use for the multiple pin on MapKit.
-(void)viewWillAppear:(BOOL)animated {
NSArray *AS=[NSArray arrayWithArray:[reports objectAtIndex:0]];
for (int i=0; i<[AS count]; i++) {
Place* home = [[[Place alloc] init] autorelease];
home.name = [[AS objectAtIndex:i] valueForKey:#"comments"];
home.latitude = [[[AS objectAtIndex:i] valueForKey:#"latitude"]floatValue];
home.longitude = [[[AS objectAtIndex:i] valueForKey:#"longitude"]floatValue];
valueForKey:#"latitude"]floatValue],[[[AS objectAtIndex:i] valueForKey:#"longitude"]floatValue]];
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
[mapView addAnnotation:from];
}
[self centerMap];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Centralized Map between two pins
-(void) centerMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 120;
CLLocationDegrees minLon = 150;
NSArray *temp=[NSArray arrayWithArray:[NSArray arrayWithArray:[reports objectAtIndex:0]]];
for (int i=0; i<[temp count];i++) {
Place* home = [[[Place alloc] init] autorelease];
home.latitude = [[[temp objectAtIndex:i] valueForKey:#"latitude"]floatValue];
home.longitude =[[[temp objectAtIndex:i] valueForKey:#"longitude"]floatValue];
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
CLLocation* currentLocation = (CLLocation*)from ;
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
}
[mapView setRegion:region animated:YES];
}
Define the multiple annotation here.
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: #"asdf"];
if (pin == nil)
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: #"asdf"] autorelease];
else
pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];
pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;
}
More details and download the sample code here.

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.

How to add ModelView Controller on Annotation Pin for showing detail UI view

I want to add detail view having Two buttons and three labels for information by taping on annotation pin. Is there any tutorial or sample code link for this. I have search but get only canShowCallout sub title which is old ways.
Edited:- I have parsed URL then on success condition I have put this code to put annotation. What should I make change in MKAnnotation view also tell me.....
for (int i = 0; i < [appDelegate.markers count]; i++)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [aMarker.lat floatValue];
location.longitude =[aMarker.lng floatValue];
AddressAnnotation *annobj = [[AddressAnnotation alloc] initWithCoordinate:location];
//[annobj setAnnotationType:AddressAnnotationTypeHettich];
//annobj.title = aMarker.name;
//annobj.subTitle = aMarker.address;
[mapView addAnnotation:annobj];
[annobj release];
CLLocationCoordinate2D ausLoc = {location.latitude,location.longitude};
//for zoom in the showroom results region
MKCoordinateSpan ausSpan = MKCoordinateSpanMake(0.108889, 0.169922);
MKCoordinateRegion ausRegion = MKCoordinateRegionMake(ausLoc, ausSpan);
NSLog(#"No Errors");
mapView.region = ausRegion;
}
indexRow = 0;
mapAnnonations = [[[NSMutableArray alloc] init] retain];
for (int i = indexRow; i<[lat ,longi count]; i++)
{
region.center.latitude = [[lat objectAtIndex:indexRow] floatValue];
region.center.longitude = [[longi objectAtIndex:indexRow] floatValue];
region.span.longitudeDelta = 0.04f;
region.span.latitudeDelta = 0.04f;
[mymapView setRegion:region animated:YES];
coordinate.latitude = [[lat objectAtIndex:indexRow] floatValue];
coordinate.longitude = [[longi objectAtIndex:indexRow]floatValue];
NSLog(#"Latitude & Longitude:---->%f,%f",coordinate.latitude,coordinate.longitude);
mapAnnotation = [[AllMapAnnotation alloc] initWithCoordinate:coordinate];
NSString *title = [[[resData valueForKey:#"name"] mutableCopy] objectAtIndex:indexRow];
NSString *titleStr = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"Pin Title:--->%#",titleStr);
[mapAnnotation setTitle:titleStr];
//[mapAnnotation setSubtitle:[NSString stringWithFormat:#"Ph: %#",pantries.phone]];
mapAnnotation.coordinate = region.center;
[mapAnnonations insertObject:mapAnnotation atIndex:indexRow];
[mymapView addAnnotations:mapAnnonations];
//[mymapView addAnnotation:mapAnnotation];
indexRow+= 1;
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;
if(annotation!= mymapView.userLocation)
{
static NSString *defaultPinID = #"pantryIdentifier";
pinView = (MKPinAnnotationView *)[mymapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
NSInteger annotationValue = [mapAnnonations indexOfObject:annotation];
NSLog(#"Annotation Value:--->%d",annotationValue);
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(40, 5, 20, 22);
[rightButton setBackgroundImage:[UIImage imageNamed:#"listtab_arrow.png"] forState:UIControlStateNormal];
rightButton.tag = annotationValue;
[rightButton addTarget:self action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
}
else
{
[mymapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
- (IBAction)showDetails:(UIView *)sender
{
NSInteger selectedIndex = sender.tag;
list = [[ListView alloc] initWithNibName:#"ListView" bundle:nil];
details = [[resData valueForKey:#"reference"] mutableCopy];
NSLog(#"Reference:--->%#",details);
list.refernceIndex = [NSString stringWithFormat:#"%#",[details objectAtIndex:selectedIndex]];
[appDelegate.navigationController pushViewController:list animated:YES];
}

IPhone SDK MapKIt Multiple Points and Annotation

I am new to IPhone SDK Development i am trying to make an application with MapKit i have done the first bit i want to add multiple pins and annotation to the application but i am lost here.
Following is the code how can i add more pins to this code
-(void)viewDidLoad{
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region={{0.0,0.0,},{0.0,0.0}};
region.center.latitude = 26.438047;
region.center.longitude = 50.116422;
region.span.latitudeDelta=0.01f;
region.span.longitudeDelta=0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #"Corporate Office";
ann.subtitle =#"King Khalid Street";
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 = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView ==nil) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor=MKPinAnnotationColorRed;
pinView.canShowCallout=YES;
pinView.animatesDrop=YES;
pinView.calloutOffset= CGPointMake(-5, 5);
}
else {
[mapView.userLocation setTitle:"I am here"];
}
return pinView;
}
you are on the right track already, just reuse your code to make multiple points.
for example :
DisplayMap *ann = [[DisplayMap alloc] init];
for( int i =1;i<=5;i++ ){
region.center.latitude = 26.438047+i;
region.center.longitude = 50.116422+i;
ann.title = [NSString stringWithFormat:#"title %d",i)];
ann.subtitle =[NSString stringWithFormat:#"subtitle %d",i)];
ann.image = [NSString stringWithFormat#"image_%d.png",i];
ann.coordinate=region.center;
[mapView addAnnotation:ann];
}
[ann release];
in result, will display 5 points in different coordinate. (with same name and subtitle).
Edited: show different pin image. you have to add new field as NSString *image to DisplayMap. and add your path image inside for loop.
- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id <MKAnnotation>) annotation
{
NSLog(#"pinnview before release %d",[pinView retainCount]);
if (pinView !=nil) {
pinView =nil;
[pinView release];
}
NSLog(#"pinnview after release %d",[pinView retainCount]);
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if(annotation != map.userLocation)
{
static NSString *defaultPinID = #"your-pin";
pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( counting < [map.annotations count])
{
counting++;
pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
for(DisplayMap* a in map.annotations)
{
if (annotation == a){
pinView.image =
[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:a.image ofType:nil]];
}
}
pinView.centerOffset= CGPointMake(0,-10);
pinView.canShowCallout = YES;
}
}
return pinView;
}