MapKit help. Can't get my location to show up first - ios

I want to develop a map that shows my location and has pins of other locations on the map.
I've gotten the map to show up and the pins to show up too, it also locates me but it doesn't zoom into my location first. How can I get it to show my location first? When I open up the mapview it just shows the last pin I put into the code.
Here is my code.
mapview.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface mapview : UIViewController {
MKMapView *mapview1;
}
#property (nonatomic, retain) IBOutletMKMapView *mapview1;
-(IBAction)setMap:(id)sender;
-(IBAction)getlocation;
#end
mapview.m
#import "mapview.h"
#import "NewClass.h"
#implementation mapview
#synthesize mapview1;
-(IBAction)getlocation {
mapview1.showsUserLocation = YES;
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview1.mapType = MKMapTypeStandard;
break;
case 1:
mapview1.mapType = MKMapTypeSatellite;
break;
case 2:
mapview1.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(void)viewDidLoad {
[super viewDidLoad];
[mapview1 setMapType:MKMapTypeStandard];
[mapview1 setZoomEnabled:YES];
[mapview1 setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 39.956907;
region.center.longitude = -75.610229;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Vigil Location";
ann.subtitle = #"Planned Parenthood of Chester County";
ann.coordinate = region.center;
[mapview1 addAnnotation:ann];
MKCoordinateRegion region1 = { {0.0, 0.0 }, {0.0, 0.0 } };
region1.center.latitude = 40.042819;
region1.center.longitude = -75.373776;
region1.span.longitudeDelta = 0.01f;
region1.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region1 animated:YES];
NewClass *ann1 = [[NewClass alloc] init];
ann1.title = #"Vigil Location";
ann1.subtitle = #"Planned Parenthood of Bryn Mawr";
ann1.coordinate = region1.center;
[mapview1 addAnnotation:ann1];
MKCoordinateRegion region2 = { {0.0, 0.0 }, {0.0, 0.0 } };
region2.center.latitude = 40.18295;
region2.center.longitude = -75.450163;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region2 animated:YES];
NewClass *ann2 = [[NewClass alloc] init];
ann2.title = #"Vigil Location";
ann2.subtitle = #"Planned Parenthood Collegeville";
ann2.coordinate = region2.center;
[mapview1 addAnnotation:ann2];
MKCoordinateRegion region3 = { {0.0, 0.0 }, {0.0, 0.0 } };
region3.center.latitude = 40.120772;
region3.center.longitude = -75.118181;
region3.span.longitudeDelta = 0.01f;
region3.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region3 animated:YES];
NewClass *ann3 = [[NewClass alloc] init];
ann3.title = #"Vigil Location";
ann3.subtitle = #"Abington Memorial Hospital";
ann3.coordinate = region3.center;
[mapview1 addAnnotation:ann3];
MKCoordinateRegion region4 = { {0.0, 0.0 }, {0.0, 0.0 } };
region4.center.latitude = 39.95361;
region4.center.longitude = -75.15267;
region4.span.longitudeDelta = 0.01f;
region4.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region4 animated:YES];
NewClass *ann4 = [[NewClass alloc] init];
ann4.title = #"Vigil Location";
ann4.subtitle = #"Philadelphia Women's Center";
ann4.coordinate = region4.center;
[mapview1 addAnnotation:ann4];
MKCoordinateRegion region5 = { {0.0, 0.0 }, {0.0, 0.0 } };
region5.center.latitude = 39.910566;
region5.center.longitude = -75.014111;
region5.span.longitudeDelta = 0.01f;
region5.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region5 animated:YES];
NewClass *ann5 = [[NewClass alloc] init];
ann5.title = #"Vigil Location";
ann5.subtitle = #"Cherry Hill Women's Center";
ann5.coordinate = region5.center;
[mapview1 addAnnotation:ann5];
MKCoordinateRegion region6 = { {0.0, 0.0 }, {0.0, 0.0 } };
region6.center.latitude = 39.74285;
region6.center.longitude = -75.550838;
region6.span.longitudeDelta = 0.01f;
region6.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region6 animated:YES];
NewClass *ann6 = [[NewClass alloc] init];
ann6.title = #"Vigil Location";
ann6.subtitle = #"Planned Parenthood of Wilmington";
ann6.coordinate = region6.center;
[mapview1 addAnnotation:ann6];
MKCoordinateRegion region7 = { {0.0, 0.0 }, {0.0, 0.0 } };
region7.center.latitude = 40.03754;
region7.center.longitude = -76.300828;
region7.span.longitudeDelta = 0.01f;
region7.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region7 animated:YES];
NewClass *ann7 = [[NewClass alloc] init];
ann7.title = #"Vigil Location";
ann7.subtitle = #"Planned Parenthood of Lancaster";
ann7.coordinate = region7.center;
[mapview1 addAnnotation:ann7];
MKCoordinateRegion region8 = { {0.0, 0.0 }, {0.0, 0.0 } };
region8.center.latitude = 40.333819;
region8.center.longitude = -75.93019;
region8.span.longitudeDelta = 0.01f;
region8.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region8 animated:YES];
NewClass *ann8 = [[NewClass alloc] init];
ann8.title = #"Vigil Location";
ann8.subtitle = #"Planned Parenthood of Reading";
ann8.coordinate = region8.center;
[mapview1 addAnnotation:ann8];
MKCoordinateRegion region9 = { {0.0, 0.0 }, {0.0, 0.0 } };
region9.center.latitude = 40.67246;
region9.center.longitude = -75.375397;
region9.span.longitudeDelta = 0.01f;
region9.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region9 animated:YES];
NewClass *ann9 = [[NewClass alloc] init];
ann9.title = #"Vigil Location";
ann9.subtitle = #"Planned Parenthood of Allentown";
ann9.coordinate = region9.center;
[mapview1 addAnnotation:ann9];
MKCoordinateRegion region10 = { {0.0, 0.0 }, {0.0, 0.0 } };
region10.center.latitude = 39.952384;
region10.center.longitude = -76.7253;
region10.span.longitudeDelta = 0.01f;
region10.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region10 animated:YES];
NewClass *ann10 = [[NewClass alloc] init];
ann10.title = #"Vigil Location";
ann10.subtitle = #"Planned Parenthood of York";
ann10.coordinate = region10.center;
[mapview1 addAnnotation:ann10];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

You've set showsUserLocation but if you read the Apple docs you'll see that that function only turns on the blue dot, it doesn't zoom or pan the view. "http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/showsUserLocation
What you want to do is set the user tracking mode to userfollows.
Other code tips:
Put all those places in an array and loop over them, don't create them one at a time like that
Don't setRegion on the map for every region you make. You're trying to get the map to zoom in on the user and each setRegion changes where the map is looking. Only the last one will have any effect anyway.
You don't need to create a region just to get the coordinates for an annotation

Related

How to set longitude and latitude using textfield

How can I set the longitude and latitude using a textfield? I have tried making my textfield a double but it just seems to crash.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = *(myTextField);
region.center.longitude = *(myTextField1);
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
How I can make this work?
MKPointAnnotation *annonation = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mycordinate;
mycordinate.latitude = [self.latitude floatValue];
mycordinate.longitude =[self.longitude floatValue];
annonation.coordinate = mycordinate;
[self.mapview addAnnotation:annonation];
In self.latitude store your latitude value and
in self.longitude store your longitude value.
Try below Code:
Just get text from textFields and set latitude,longitude float value from text.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = [self.myTextField.text floatValue];
region.center.longitude = [self.myTextField1.text floatValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}

Drop Multiple Pins on Maps, Xcode

so I have this code which drops a single Pin in google maps on my iPhone app.
Do you know how I would be able to drop multiple Pins to show the different locations of the business?
#import "NewClass.h"
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = 40.707184;
region.center.longitude = -73.998392;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Brooklyn Bridge";
ann.subtitle = #"New York";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
My duplication attempt
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = 53.0838491;
region.center.longitude = -2.7998707;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Making Waves Swim School";
ann.subtitle = #"Clutton, Cheshire";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
MKCoordinateRegion region2 = { {0.0, 0.0}, {0.0,0.0}};
region2.center.latitude = 53.199801;
region2.center.longitude = -2.89744;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.01f;
[mapview setRegion:region2 animated:YES];
NewClass *ann2 = [[NewClass alloc] init];
ann2.title = #"Chester Uni";
ann2.subtitle = #"Chester, Cheshire";
ann2.coordinate = region.center;
[mapview addAnnotation:ann2];
ann2.coordinate = region.center;
should be
ann2.coordinate = region2.center;
So that the 2 pins are not having the exact same coordinates.

iOS Displaying annotations from an array

I am trying to display multiple annotations on my mapView from an array. The annotation coordinates are parsed from an XML file and stored in the array as [currentCall longitude] and [currentCall latitude]. My question is, what is the syntax for "calling" the array? (I don't know if that's how you say it). In another part of my application, I display the parsed XML results in a table and use "JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];" to "call" the array. How do I do it for displaying my annotations? Everything else works fine except that little part.
Here is the implementation file:
#implementation SecondViewController
#synthesize mapView;
XMLParser *xmlParser;
-(IBAction)getlocation {
mapView.showsUserLocation = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}
-(IBAction)changeSeg:(id)sender {
if (segment.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}
if (segment.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeSatellite;
}
if (segment.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeHybrid;
}
}
-(void)viewDidLoad {
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion WCCCA = { {0.0, 0.0} , {0.0, 0.0} };
WCCCA.center.latitude = 45.53540820864449;
WCCCA.center.longitude = -122.86178648471832;
WCCCA.span.longitudeDelta = 0.02f;
WCCCA.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"WCCCA";
ann1.subtitle = #"Washington County Consolidated Communications Agency";
ann1.coordinate = WCCCA.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion CALL = { {0.0, 0.0} , {0.0, 0.0} };
CALL.center.latitude = [currentCall.latitude doubleValue];
CALL.center.longitude = [currentCall.longitude doubleValue];
CALL.span.longitudeDelta = 0.02f;
CALL.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = [currentCall currentCallType];
ann2.subtitle = [currentCall location];
ann2.coordinate = CALL.center;
[mapView addAnnotation: ann1];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorRed;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
#end
If I am understanding your code properly, you should be able to just iterate through your parsed XML (it appears that the output the from your xmlParser object is an NSArray) and add the annotations to the mapView within the loop.
You can use the C function CLLocationCoordinate2DMake() to create the coordinate data structure for your annotation.
NSArray *callsArray = [xmlParser calls];
for (JointCAD *call in callsArray) {
Annotation *ann = [[Annotation alloc] init];
ann.title = [call currentCallType];
ann.subtitle = [call location];
ann.coordinate = CLLocationCoordinate2DMake([call latitude], [call longitude]);
[mapView addAnnotation:ann];
}

TableView & MapView same Detail View

I have a table view that load the data from an xml in a array and show it, every cell point to the same detail view repopulated.
In the same view i have a hidden map view, the map view show the same array of the table in the map, every annotation have a disclosure button that point to the same detail view of the table, but i don't understand how i can pass the data from the map view to the detail view...
for the disclosure button i use this code
-(IBAction)showDetails:(id)sender{
DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:#"DettMercatiViewController" bundle:nil];
dettMercatiViewController.mieiMercati = [table objectAtIndex:????];
[self.navigationController pushViewController:dettMercatiViewController animated:YES];
[dettMercatiViewController release];
}
table is the name of the nsmutablearray created from the xml but i don't know what objectIndex use... any idea?
ok, here the viewdidload and the creation of pin, please help me and explain because i am desolate:
- (void)viewDidLoad
{
pp =0;
//Colore NavigationBar
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:33.0f/255.0f green:89.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
//Font Navigation Bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Museo700-Regular" size:20];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=[self.title uppercaseString];
self.navigationItem.titleView = label;
[label release];
//creazione nome XML
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *nomeXML = [NSString stringWithFormat: #"%#_%#_%#",[defaults stringForKey:#"interesse"],[defaults stringForKey:#"provincia"],[defaults stringForKey:#"giorno"]];
NSLog(#"%#", nomeXML);
//Caricamento XML
table = [[NSMutableArray alloc] init];
NSString *url = [[NSBundle mainBundle] pathForResource:nomeXML ofType:#"xml"];
XMLParser *myParser = [[[XMLParser alloc]autorelease] parseXMLAtURL:url toObject:#"Mercato" parseError:nil];
for(int i = 0; i < [[myParser items] count]; i++) {
Mercato *new = [[myParser items] objectAtIndex:i];
[table addObject:new];
///////////////////////////////////////////
//definizione posizione utente
AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
CLLocation *userLoc = appDelegate.locationManager.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(#"user latitude = %f",userCoordinate.latitude);
NSLog(#"user longitude = %f",userCoordinate.longitude);
mapView.delegate=self;
//Pin Mappa
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [new.latitudine doubleValue];
theCoordinate.longitude = [new.longitudine doubleValue];
myAnnotation=[[MyAnnotation alloc] init];
myAnnotation.coordinate=theCoordinate;
myAnnotation.title=[NSString stringWithFormat:#"%#", new.ubicazione];
myAnnotation.subtitle=[NSString stringWithFormat:#"%#, %#, %#",new.comune, new.giorno, new. orario];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
if([new.latitudine doubleValue] == 0) {
[mapView removeAnnotation:myAnnotation];
myAnnotation = nil;
}
// Inizializza mappa al Centro della Provincia
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"bergamo"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.6982642;
newRegion.center.longitude = 9.6772698;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"brescia"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.5411875;
newRegion.center.longitude = 10.2194437;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"como"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8080597;
newRegion.center.longitude = 9.0851765;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"cremona"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.133249;
newRegion.center.longitude = 10.0226511;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"lecco"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8565698;
newRegion.center.longitude = 9.3976704;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"lodi"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.3138041;
newRegion.center.longitude = 9.5018274;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"mantova"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.1564168;
newRegion.center.longitude = 10.7913751;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"milano"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.4654542;
newRegion.center.longitude = 9.186516;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.512872;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"monza e brianza"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.623599;
newRegion.center.longitude = 9.2588015;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"pavia"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.1847248;
newRegion.center.longitude = 9.1582069;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"sondrio"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 46.1698583;
newRegion.center.longitude = 9.8787674;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"varese"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8205989;
newRegion.center.longitude = 8.8250576;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
}
////////////////////////////
[super viewDidLoad];
}
#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(#"welcome into the map view annotation");
pp++;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:AnnotationIdentifier]autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorGreen;
pinView.tag = pp;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:myAnnotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
////////////////
-(IBAction)showDetails:(id)sender{
NSLog(#"%i", pp);
DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:#"DettMercatiViewController" bundle:nil];
dettMercatiViewController.mieiMercati = [table objectAtIndex:[sender tag]];
[self.navigationController pushViewController:dettMercatiViewController animated:YES];
[dettMercatiViewController release];
}
I suggest to give every disclosure button in your map view a tag and you can use this tag than
so
[table objectAtIndex:[sender tag]];

Two Annotation Pins at a time in MKMapView

I successfully implemented MKMapVIew and a single annotation on my Map. I am not able to represent two postitions simultaneously. I am using MKMapViewDelegate method :
mapView:viewForAnnotation:
Can someone look into this thing.
Thanks!
EDIT
- (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 = 22.569722 ;
region.center.longitude = 88.369722;
region.span.longitudeDelta = 0.1f;
region.span.latitudeDelta = 0.1f;
MKCoordinateRegion anotherRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
anotherRegion.center.latitude = 28.38 ;
anotherRegion.center.longitude = 77.12;
anotherRegion.span.longitudeDelta = 90.0f;
anotherRegion.span.latitudeDelta = 90.0f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #" Kolkata";
ann.subtitle = #"Mahatma Gandhi Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
DisplayAnotherMap *annMap = [[DisplayAnotherMap alloc] init];
annMap.title = #" New Delhi";
annMap.subtitle = #"Shahdara";
annMap.coordinate = anotherRegion.center;
[mapView addAnnotations:[NSArray arrayWithObjects:annMap,ann,nil]];
}
This will fulfill the requirement for you! ...:)
The method mapView:viewForAnnotation: is just for the view of a the annotations, eg. the colour of the pin and the title label etc. If you want to show multiple annotations you should alloc and init the all with the required positions. For example, you could create a .plist with all the coordinates and the in for cycle just add them.
EDIT
This is a sample code, taken from somewhere. You must alloc and init all anotations.
-(void)loadDummyPlaces{
srand((unsigned)time(0));
NSMutableArray *tempPlaces=[[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<1000; i++) {
MyPlace *place=[[MyPlace alloc] initWithCoordinate:CLLocationCoordinate2DMake([self RandomFloatStart:42.0 end:47.0],[self RandomFloatStart:14.0 end:19.0])];
[place setCurrentTitle:[NSString stringWithFormat:#"Place %d title",i]];
[place setCurrentSubTitle:[NSString stringWithFormat:#"Place %d subtitle",i]];
[place addPlace:place];
[tempPlaces addObject:place];
[place release];
}
places=[[NSArray alloc] initWithArray:tempPlaces];
[tempPlaces release];
}

Resources